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

187 lines
6.5 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"id": "373ed1db",
"metadata": {},
"source": [
"# Exercise: Match the tarot cards!\n",
"\n",
"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`.\n",
"\n",
"For example:\n",
"```\n",
"deck1 = ['C', 'B', 'A']\n",
"deck2 = ['A', 'C', 'B']\n",
"```\n",
"\n",
"should return (in no particular order):\n",
"\n",
"```\n",
"{(0, 1), (1, 2), (2, 0)}\n",
"```\n",
"\n",
"1. Write an algorithm to match the tarot cards\n",
"2. Compute the Big-O complexity of your algorithm\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cf05b9c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-- Deck 1: --\n",
" ['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']\n",
"-- Deck 2: --\n",
" ['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']\n"
]
}
],
"source": [
"import random\n",
"\n",
"# List of tarot card names (Major Arcana)\n",
"tarot_cards = [\n",
" \"The Fool\", \"The Magician\", \"The High Priestess\", \"The Empress\", \"The Emperor\",\n",
" \"The Hierophant\", \"The Lovers\", \"The Chariot\", \"Strength\", \"The Hermit\",\n",
" \"Wheel of Fortune\", \"Justice\", \"The Hanged Man\", \"Death\", \"Temperance\",\n",
" \"The Devil\", \"The Tower\", \"The Star\", \"The Moon\", \"The Sun\", \"Judgement\",\n",
" \"The World\"\n",
"]\n",
"\n",
"# Copy the list to create two separate decks\n",
"deck1 = tarot_cards.copy()\n",
"deck2 = tarot_cards.copy()\n",
"\n",
"# Shuffle both decks\n",
"random.shuffle(deck1)\n",
"random.shuffle(deck2)\n",
"\n",
"# Print the shuffled decks\n",
"print(\"-- Deck 1: --\\n\", deck1)\n",
"print(\"-- Deck 2: --\\n\", deck2)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a5fb5054-5c4a-4973-9e72-300003d69a53",
"metadata": {},
"outputs": [],
"source": [
"deck2 = dict((card, idx) for (idx, card) in enumerate(deck2))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "dd077f0a-c858-4257-8d72-a3dd4844074f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{('The Hierophant', 18): 0,\n",
" ('The Magician', 3): 1,\n",
" ('The Hanged Man', 2): 2,\n",
" ('The Lovers', 11): 3,\n",
" ('Judgement', 13): 4,\n",
" ('The Sun', 17): 5,\n",
" ('The High Priestess', 16): 6,\n",
" ('The Hermit', 10): 7,\n",
" ('The Star', 21): 8,\n",
" ('Temperance', 6): 9,\n",
" ('The World', 5): 10,\n",
" ('The Fool', 19): 11,\n",
" ('Strength', 20): 12,\n",
" ('The Devil', 0): 13,\n",
" ('Justice', 7): 14,\n",
" ('The Emperor', 8): 15,\n",
" ('The Moon', 1): 16,\n",
" ('Wheel of Fortune', 14): 17,\n",
" ('The Tower', 12): 18,\n",
" ('Death', 15): 19,\n",
" ('The Empress', 9): 20,\n",
" ('The Chariot', 4): 21}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deck2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "48eb31e2",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'The Chariot'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[19], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mmatch\u001b[39;00m\u001b[38;5;241m.\u001b[39madd(idx, deck2[card1])\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m match\n\u001b[0;32m----> 9\u001b[0m match \u001b[38;5;241m=\u001b[39m \u001b[43mmatch_tarot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdeck1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdeck2\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mmatch\u001b[39;00m\n",
"Cell \u001b[0;32mIn[19], line 6\u001b[0m, in \u001b[0;36mmatch_tarot\u001b[0;34m(deck1, deck2)\u001b[0m\n\u001b[1;32m 4\u001b[0m match \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m()\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m (card1, idx) \u001b[38;5;129;01min\u001b[39;00m deck1\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mmatch\u001b[39;00m\u001b[38;5;241m.\u001b[39madd(idx, \u001b[43mdeck2\u001b[49m\u001b[43m[\u001b[49m\u001b[43mcard1\u001b[49m\u001b[43m]\u001b[49m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m match\n",
"\u001b[0;31mKeyError\u001b[0m: 'The Chariot'"
]
}
],
"source": [
"def match_tarot(deck1, deck2):\n",
" deck1 = dict((card, idx) for (idx, card) in enumerate(deck1))\n",
" deck2 = dict((card, idx) for (idx, card) in enumerate(deck2))\n",
" match = set()\n",
" for (card1, idx) in deck1.items():\n",
" if card1 in deck2.keys()\n",
" match.add(idx, deck2[card1])\n",
" return match\n",
"\n",
"match = match_tarot(deck1, deck2)\n",
"match"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "509dda71",
"metadata": {},
"outputs": [],
"source": [
"match ={}\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}