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

3.7 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 [1]:
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 Star', 'Justice', 'The Hanged Man', 'The Magician', 'The Hierophant', 'The High Priestess', 'The Chariot', 'The Emperor', 'The Fool', 'The Tower', 'The Devil', 'Strength', 'Judgement', 'The Moon', 'The Empress', 'The World', 'Wheel of Fortune', 'Death', 'The Lovers', 'The Sun', 'Temperance', 'The Hermit']
-- Deck 2: --
 ['The Lovers', 'The Chariot', 'Strength', 'The High Priestess', 'The Hermit', 'Justice', 'The Fool', 'Death', 'The Emperor', 'The Devil', 'The Star', 'The Sun', 'Judgement', 'The Hanged Man', 'The Hierophant', 'Wheel of Fortune', 'The Moon', 'The Magician', 'The Empress', 'Temperance', 'The Tower', 'The World']
In [11]:
deck1_dict = {key:value for key,value in zip(deck1, list(range(len(deck1))))}
 deck2_dict = {key:value for key,value in zip(deck2, list(range(len(deck1))))}
result = set()
for card in tarot_cards:
    result.add((deck1_dict[card], deck2_dict[card]))

print(result)
{(15, 21), (11, 2), (8, 6), (10, 9), (9, 20), (12, 12), (20, 19), (4, 14), (5, 3), (14, 18), (19, 11), (0, 10), (1, 5), (6, 1), (18, 0), (13, 16), (2, 13), (16, 15), (17, 7), (3, 17), (21, 4), (7, 8)}