190 lines
5 KiB
Text
190 lines
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": 1,
|
|
"id": "cf05b9c4",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-- Deck 1: --\n",
|
|
" ['The Hanged Man', 'The Emperor', 'Death', 'The Hierophant', 'The Magician', 'Strength', 'The Sun', 'Temperance', 'The World', 'The Lovers', 'The Moon', 'The Devil', 'The Hermit', 'Judgement', 'Justice', 'Wheel of Fortune', 'The Empress', 'The Fool', 'The Chariot', 'The Tower', 'The Star', 'The High Priestess']\n",
|
|
"-- Deck 2: --\n",
|
|
" ['The Hermit', 'The High Priestess', 'The Magician', 'The Hanged Man', 'Judgement', 'The Lovers', 'The Emperor', 'The Sun', 'The Tower', 'The Chariot', 'The Moon', 'Wheel of Fortune', 'The World', 'Temperance', 'The Empress', 'The Devil', 'Strength', 'Death', 'The Hierophant', 'The Star', 'Justice', 'The Fool']\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 find_idx(card_name, deck1, deck2): \n",
|
|
" \n",
|
|
" idx1 = 0\n",
|
|
" for i,card in enumerate(deck1):\n",
|
|
" if card_name == card:\n",
|
|
" idx1 = i\n",
|
|
" break\n",
|
|
" idx2 = 0\n",
|
|
" for i, card in enumerate(deck2):\n",
|
|
" if card_name == card:\n",
|
|
" idx2 = i\n",
|
|
" break\n",
|
|
" \n",
|
|
" return (idx1,idx2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "c854c656-b41e-43ca-9fa1-e06b04b33944",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[(0, 3),\n",
|
|
" (1, 6),\n",
|
|
" (2, 17),\n",
|
|
" (3, 18),\n",
|
|
" (4, 2),\n",
|
|
" (5, 16),\n",
|
|
" (6, 7),\n",
|
|
" (7, 13),\n",
|
|
" (8, 12),\n",
|
|
" (9, 5),\n",
|
|
" (10, 10),\n",
|
|
" (11, 15),\n",
|
|
" (12, 0),\n",
|
|
" (13, 4),\n",
|
|
" (14, 20),\n",
|
|
" (15, 11),\n",
|
|
" (16, 14),\n",
|
|
" (17, 21),\n",
|
|
" (18, 9),\n",
|
|
" (19, 8),\n",
|
|
" (20, 19),\n",
|
|
" (21, 1)]"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"deck1_dict = {card: i for i,card in enumerate(deck1)}\n",
|
|
"deck2_dict = {card: i for i,card in enumerate(deck2)}\n",
|
|
"\n",
|
|
"idx_pairs = []\n",
|
|
"for card,idx1 in deck1_dict.items():\n",
|
|
" idx2 = deck2_dict[card]\n",
|
|
"\n",
|
|
" idx_pairs.append((idx1,idx2))\n",
|
|
"\n",
|
|
"idx_pairs\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "509dda71",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(17, 21)"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"card_name = \"The Fool\"\n",
|
|
"\n",
|
|
"find_idx(card_name, deck1, deck2)"
|
|
]
|
|
}
|
|
],
|
|
"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.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|