133 lines
3.9 KiB
Text
133 lines
3.9 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": 2,
|
|
"id": "cf05b9c4",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-- Deck 1: --\n",
|
|
" ['The World', 'Wheel of Fortune', 'The Chariot', 'The Magician', 'The Hierophant', 'The Lovers', 'Judgement', 'Death', 'The Empress', 'The Hanged Man', 'The Hermit', 'The Star', 'Strength', 'The Sun', 'Temperance', 'The Devil', 'The Emperor', 'The High Priestess', 'The Moon', 'Justice', 'The Tower', 'The Fool']\n",
|
|
"-- Deck 2: --\n",
|
|
" ['The Moon', 'The Fool', 'The Hanged Man', 'The Empress', 'The Tower', 'Strength', 'Temperance', 'The Magician', 'Judgement', 'Wheel of Fortune', 'The Devil', 'Justice', 'The Lovers', 'The Hierophant', 'The Chariot', 'The Sun', 'Death', 'The Star', 'The High Priestess', 'The Emperor', 'The World', 'The Hermit']\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": 4,
|
|
"id": "48eb31e2",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[(0, 20), (1, 9), (2, 14), (3, 7), (4, 13), (5, 12), (6, 8), (7, 16), (8, 3), (9, 2), (10, 21), (11, 17), (12, 5), (13, 15), (14, 6), (15, 10), (16, 19), (17, 18), (18, 0), (19, 11), (20, 4), (21, 1)]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"pairs = []\n",
|
|
"for idx1, card1 in enumerate(deck1):\n",
|
|
" for idx2, card2 in enumerate(deck2):\n",
|
|
" if card1 == card2:\n",
|
|
" pairs.append((idx1, idx2))\n",
|
|
"\n",
|
|
"print(pairs)\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9c15f5b0-373c-4ae6-82c5-efc97a95f0a5",
|
|
"metadata": {},
|
|
"source": [
|
|
"O(n^2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "622c6741-accc-484b-99dc-e0dda1fc1193",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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
|
|
}
|