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

165 lines
4.3 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": 3,
"id": "cf05b9c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-- Deck 1: --\n",
" ['The Hierophant', 'Temperance', 'Wheel of Fortune', 'The Star', 'The Sun', 'The Tower', 'The Moon', 'Strength', 'The Hanged Man', 'The Hermit', 'The Devil', 'The Fool', 'The Magician', 'The Empress', 'Justice', 'The Lovers', 'The Emperor', 'The Chariot', 'The World', 'Judgement', 'Death', 'The High Priestess']\n",
"-- Deck 2: --\n",
" ['Wheel of Fortune', 'Justice', 'The World', 'Temperance', 'The Fool', 'The Tower', 'The Sun', 'The Moon', 'The Chariot', 'The Devil', 'The Empress', 'The Hanged Man', 'Strength', 'Judgement', 'The Hierophant', 'The Lovers', 'The Emperor', 'The Magician', 'The Hermit', 'The Star', 'The High Priestess', 'Death']\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": 2,
"id": "48eb31e2",
"metadata": {},
"outputs": [],
"source": [
"def match_cards(deck1_, deck2_):\n",
" res = []\n",
" for i,card in enumerate(deck1_):\n",
" idx2 = deck2_.index(card)\n",
" res.append((i,idx2))\n",
" return set(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01932720",
"metadata": {},
"outputs": [],
"source": [
"#O(n^2)??"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6dc5dbcf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(0, 14),\n",
" (1, 3),\n",
" (2, 0),\n",
" (3, 19),\n",
" (4, 6),\n",
" (5, 5),\n",
" (6, 7),\n",
" (7, 12),\n",
" (8, 11),\n",
" (9, 18),\n",
" (10, 9),\n",
" (11, 4),\n",
" (12, 17),\n",
" (13, 10),\n",
" (14, 1),\n",
" (15, 15),\n",
" (16, 16),\n",
" (17, 8),\n",
" (18, 2),\n",
" (19, 13),\n",
" (20, 21),\n",
" (21, 20)}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"match_cards(deck1,deck2)"
]
},
{
"cell_type": "markdown",
"id": "509dda71",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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
}