2024-heraklion-data/exercises/.ipynb_checkpoints/match_tarots-checkpoint.ipynb

169 lines
4.4 KiB
Plaintext
Raw Permalink Normal View History

2024-08-27 14:27:53 +02:00
{
"cells": [
{
"cell_type": "markdown",
"id": "373ed1db",
"metadata": {},
"source": [
"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`.\n",
"\n",
"For examples:\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",
"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: ['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']\n",
"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']\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:\", deck1)\n",
"print(\"Deck 2:\", deck2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7685740c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 12],\n",
" [1, 11],\n",
" [2, 16],\n",
" [3, 10],\n",
" [4, 14],\n",
" [5, 1],\n",
" [6, 0],\n",
" [7, 7],\n",
" [8, 2],\n",
" [9, 18],\n",
" [10, 8],\n",
" [11, 5],\n",
" [12, 3],\n",
" [13, 21],\n",
" [14, 15],\n",
" [15, 19],\n",
" [16, 4],\n",
" [17, 20],\n",
" [18, 13],\n",
" [19, 17],\n",
" [20, 9],\n",
" [21, 6]]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card_to_matches = {}\n",
"for card in deck1: # O(N)\n",
" card_to_matches[card] = [None, None] # O(1)\n",
"\n",
"for idx1, card in enumerate(deck1): # O(N)\n",
" card_to_matches[card][0] = idx1 # O(1)\n",
" \n",
"for idx2, card in enumerate(deck2): # O(N)\n",
" card_to_matches[card][1] = idx2 # O(1)\n",
" \n",
"list(card_to_matches.values()) # O(N)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2b33252c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The World'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "509dda71",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}