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

5.2 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 Lovers', 'Wheel of Fortune', 'Strength', 'The Moon', 'The Emperor', 'The Star', 'The High Priestess', 'The Hanged Man', 'The Sun', 'The Devil', 'The Empress', 'The Fool', 'The Chariot', 'The Magician', 'Judgement', 'The World', 'Temperance', 'The Hermit', 'The Hierophant', 'The Tower', 'Death', 'Justice']
-- Deck 2: --
 ['Judgement', 'The High Priestess', 'The Moon', 'The World', 'The Hermit', 'The Star', 'The Hierophant', 'Death', 'The Hanged Man', 'The Devil', 'The Emperor', 'The Empress', 'The Tower', 'Temperance', 'Justice', 'The Fool', 'Strength', 'The Magician', 'The Lovers', 'Wheel of Fortune', 'The Sun', 'The Chariot']
In [ ]:

In [ ]:

In [2]:
def make_dict_deck(deck):
    dict_deck = {}
    for i in range(len(deck)):
        name = deck[i]
        dict_deck[name] = i
    return dict_deck
In [8]:
def compare_decks(dict1, dict2):
    indeces = []
    for name in dict1.keys():
        if name in dict2.keys():
            indeces.append((dict1[name], dict2[name]))
    return indeces
In [9]:
dict1 = make_dict_deck(deck1)
dict2 = make_dict_deck(deck2)
In [10]:
compare_decks(dict1, dict2)
Out[10]:
[(0, 18),
 (1, 19),
 (2, 16),
 (3, 2),
 (4, 10),
 (5, 5),
 (6, 1),
 (7, 8),
 (8, 20),
 (9, 9),
 (10, 11),
 (11, 15),
 (12, 21),
 (13, 17),
 (14, 0),
 (15, 3),
 (16, 13),
 (17, 4),
 (18, 6),
 (19, 12),
 (20, 7),
 (21, 14)]
In [ ]: