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

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

matching = []

for indx, card in enumerate(deck2):
    if card in set1:
        matching.append((deck2.index(card), deck1.index(card)))
In [11]:
matching
Out[11]:
[(0, 3),
 (1, 20),
 (2, 17),
 (3, 16),
 (4, 4),
 (5, 8),
 (6, 6),
 (7, 11),
 (8, 5),
 (9, 7),
 (10, 12),
 (11, 21),
 (12, 2),
 (13, 9),
 (14, 0),
 (15, 15),
 (16, 1),
 (17, 14),
 (18, 13),
 (19, 19),
 (20, 18),
 (21, 10)]
In [ ]: