2024-heraklion-data/exercises/.ipynb_checkpoints/match_tarots-checkpoint.ipynb
2024-08-27 15:27:53 +03:00

4.4 KiB

Exercise: given 2 decks of tarod 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 examples:

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

should return (in no particular order):

{(0, 1), (1, 2), (2, 0)}

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:", deck1)
print("Deck 2:", deck2)
Deck 1: ['The Fool', 'Death', 'The Lovers', 'Judgement', 'Temperance', 'The Sun', 'The Hermit', 'The Chariot', 'The Tower', 'Wheel of Fortune', 'The Devil', 'The Emperor', 'The Empress', 'The World', 'The Hierophant', 'The High Priestess', 'The Star', 'The Hanged Man', 'Strength', 'Justice', 'The Moon', 'The Magician']
Deck 2: ['The Hermit', 'The Sun', 'The Tower', 'The Empress', 'The Star', 'The Emperor', 'The Magician', 'The Chariot', 'The Devil', 'The Moon', 'Judgement', 'Death', 'The Fool', 'Strength', 'Temperance', 'The Hierophant', 'The Lovers', 'Justice', 'Wheel of Fortune', 'The High Priestess', 'The Hanged Man', 'The World']
In [6]:
card_to_matches = {}
for card in deck1:  # O(N)
    card_to_matches[card] = [None, None]  # O(1)

for idx1, card in enumerate(deck1):  # O(N)
    card_to_matches[card][0] = idx1  # O(1)
    
for idx2, card in enumerate(deck2):  # O(N)
    card_to_matches[card][1] = idx2  # O(1)
    
list(card_to_matches.values())  # O(N)
Out[6]:
[[0, 12],
 [1, 11],
 [2, 16],
 [3, 10],
 [4, 14],
 [5, 1],
 [6, 0],
 [7, 7],
 [8, 2],
 [9, 18],
 [10, 8],
 [11, 5],
 [12, 3],
 [13, 21],
 [14, 15],
 [15, 19],
 [16, 4],
 [17, 20],
 [18, 13],
 [19, 17],
 [20, 9],
 [21, 6]]
In [5]:
card
Out[5]:
'The World'
In [ ]: