{ "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 Lovers', 'Wheel of Fortune', 'Strength', 'The Moon', 'The Emperor', 'The Star', 'The High Priestess', 'The Hanged Man', 'The Sun', 'The Devil', 'The Empress', 'The Fool', 'The Chariot', 'The Magician', 'Judgement', 'The World', 'Temperance', 'The Hermit', 'The Hierophant', 'The Tower', 'Death', 'Justice']\n", "-- Deck 2: --\n", " ['Judgement', 'The High Priestess', 'The Moon', 'The World', 'The Hermit', 'The Star', 'The Hierophant', 'Death', 'The Hanged Man', 'The Devil', 'The Emperor', 'The Empress', 'The Tower', 'Temperance', 'Justice', 'The Fool', 'Strength', 'The Magician', 'The Lovers', 'Wheel of Fortune', 'The Sun', 'The Chariot']\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": null, "id": "58d631bc-84ae-4715-8468-db6d01eb1539", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8674444d-47d2-4137-8d5c-4d31a20c8f33", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "id": "48eb31e2", "metadata": {}, "outputs": [], "source": [ "def make_dict_deck(deck):\n", " dict_deck = {}\n", " for i in range(len(deck)):\n", " name = deck[i]\n", " dict_deck[name] = i\n", " return dict_deck" ] }, { "cell_type": "code", "execution_count": 8, "id": "3f925be7-9cf3-40b7-9597-929729e62c42", "metadata": {}, "outputs": [], "source": [ "def compare_decks(dict1, dict2):\n", " indeces = []\n", " for name in dict1.keys():\n", " if name in dict2.keys():\n", " indeces.append((dict1[name], dict2[name]))\n", " return indeces" ] }, { "cell_type": "code", "execution_count": 9, "id": "509dda71", "metadata": {}, "outputs": [], "source": [ "dict1 = make_dict_deck(deck1)\n", "dict2 = make_dict_deck(deck2)" ] }, { "cell_type": "code", "execution_count": 10, "id": "a54c0d47-609f-4860-92bc-96787514e293", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 18),\n", " (1, 19),\n", " (2, 16),\n", " (3, 2),\n", " (4, 10),\n", " (5, 5),\n", " (6, 1),\n", " (7, 8),\n", " (8, 20),\n", " (9, 9),\n", " (10, 11),\n", " (11, 15),\n", " (12, 21),\n", " (13, 17),\n", " (14, 0),\n", " (15, 3),\n", " (16, 13),\n", " (17, 4),\n", " (18, 6),\n", " (19, 12),\n", " (20, 7),\n", " (21, 14)]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "compare_decks(dict1, dict2)" ] }, { "cell_type": "code", "execution_count": null, "id": "81541f74-bf7a-4203-b3ab-67a3962ad72d", "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 }