2025-plovdiv-data/exercises/match_tarots/match_tarots.ipynb
2025-09-24 10:50:46 +03:00

6.5 KiB

Exercise: Match the tarot cards!

Given 2 decks of tarot cards, deck1 and deck2, find all the matching pairs. The output should be a set of tuples (idx1, idx2) for every matching pair in deck1, deck2.

For example:

deck1 = ['C', 'B', 'A']
deck2 = ['A', 'C', 'B']

should return (in no particular order):

{(0, 1), (1, 2), (2, 0)}
  1. Write an algorithm to match the tarot cards
  2. Compute the Big-O complexity of your algorithm
In [7]:
import random

# List of tarot card names (Major Arcana)
tarot_cards = [
    "The Fool", "The Magician", "The High Priestess", "The Empress", "The Emperor",
    "The Hierophant", "The Lovers", "The Chariot", "Strength", "The Hermit",
    "Wheel of Fortune", "Justice", "The Hanged Man", "Death", "Temperance",
    "The Devil", "The Tower", "The Star", "The Moon", "The Sun", "Judgement",
    "The World"
]

# Copy the list to create two separate decks
deck1 = tarot_cards.copy()
deck2 = tarot_cards.copy()

# Shuffle both decks
random.shuffle(deck1)
random.shuffle(deck2)

# Print the shuffled decks
print("-- Deck 1: --\n", deck1)
print("-- Deck 2: --\n", deck2)
-- Deck 1: --
 ['The Chariot', 'Strength', 'The Lovers', 'The Moon', 'The Magician', 'Judgement', 'Wheel of Fortune', 'The Tower', 'The Hierophant', 'The Star', 'The Fool', 'The Empress', 'The Hanged Man', 'The Sun', 'The Emperor', 'The High Priestess', 'The Hermit', 'The World', 'Death', 'Temperance', 'Justice', 'The Devil']
-- Deck 2: --
 ['The Devil', 'The Moon', 'The Hanged Man', 'The Magician', 'The Chariot', 'The World', 'Temperance', 'Justice', 'The Emperor', 'The Empress', 'The Hermit', 'The Lovers', 'The Tower', 'Judgement', 'Wheel of Fortune', 'Death', 'The High Priestess', 'The Sun', 'The Hierophant', 'The Fool', 'Strength', 'The Star']
In [15]:
deck2 = dict((card, idx) for (idx, card) in enumerate(deck2))
In [16]:
deck2
Out[16]:
{('The Hierophant', 18): 0,
 ('The Magician', 3): 1,
 ('The Hanged Man', 2): 2,
 ('The Lovers', 11): 3,
 ('Judgement', 13): 4,
 ('The Sun', 17): 5,
 ('The High Priestess', 16): 6,
 ('The Hermit', 10): 7,
 ('The Star', 21): 8,
 ('Temperance', 6): 9,
 ('The World', 5): 10,
 ('The Fool', 19): 11,
 ('Strength', 20): 12,
 ('The Devil', 0): 13,
 ('Justice', 7): 14,
 ('The Emperor', 8): 15,
 ('The Moon', 1): 16,
 ('Wheel of Fortune', 14): 17,
 ('The Tower', 12): 18,
 ('Death', 15): 19,
 ('The Empress', 9): 20,
 ('The Chariot', 4): 21}
In [19]:
def match_tarot(deck1, deck2):
    deck1 = dict((card, idx) for (idx, card) in enumerate(deck1))
    deck2 = dict((card, idx) for (idx, card) in enumerate(deck2))
    match = set()
    for (card1, idx) in deck1.items():
        if card1 in deck2.keys()
        match.add(idx, deck2[card1])
    return match

match = match_tarot(deck1, deck2)
match
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[19], line 9
      6         match.add(idx, deck2[card1])
      7     return match
----> 9 match = match_tarot(deck1, deck2)
     10 match

Cell In[19], line 6, in match_tarot(deck1, deck2)
      4 match = set()
      5 for (card1, idx) in deck1.items():
----> 6     match.add(idx, deck2[card1])
      7 return match

KeyError: 'The Chariot'
In [ ]:
match ={}