{
 "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 Tower', 'The Star', 'Strength', 'The Devil', 'Judgement', 'The World', 'The High Priestess', 'The Hanged Man', 'The Sun', 'The Lovers', 'The Chariot', 'The Emperor', 'The Fool', 'The Empress', 'Death', 'Temperance', 'Justice', 'The Magician', 'Wheel of Fortune', 'The Hermit', 'The Hierophant', 'The Moon']\n",
      "-- Deck 2: --\n",
      " ['Temperance', 'The Sun', 'The Lovers', 'Strength', 'The High Priestess', 'The Magician', 'Justice', 'Judgement', 'The Empress', 'The Star', 'The Fool', 'The Hierophant', 'The Hanged Man', 'The Tower', 'The Moon', 'The Chariot', 'Death', 'The World', 'The Hermit', 'The Devil', 'Wheel of Fortune', 'The Emperor']\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": "markdown",
   "id": "3db3f337",
   "metadata": {},
   "source": [
    "# Simplest implementation: O(N²)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "319ef6a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 13),\n",
       " (1, 9),\n",
       " (2, 3),\n",
       " (3, 19),\n",
       " (4, 7),\n",
       " (5, 17),\n",
       " (6, 4),\n",
       " (7, 12),\n",
       " (8, 1),\n",
       " (9, 2),\n",
       " (10, 15),\n",
       " (11, 21),\n",
       " (12, 10),\n",
       " (13, 8),\n",
       " (14, 16),\n",
       " (15, 0),\n",
       " (16, 6),\n",
       " (17, 5),\n",
       " (18, 20),\n",
       " (19, 18),\n",
       " (20, 11),\n",
       " (21, 14)}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matches = set()\n",
    "for idx1, card in enumerate(deck1):    # O(N)\n",
    "    match = (idx1, deck2.index(card))  # O(N)\n",
    "    matches.add(match)\n",
    "    \n",
    "matches"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3264eb67",
   "metadata": {},
   "source": [
    "# Faster solution: O(N log N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c768a7c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 13),\n",
       " (1, 9),\n",
       " (2, 3),\n",
       " (3, 19),\n",
       " (4, 7),\n",
       " (5, 17),\n",
       " (6, 4),\n",
       " (7, 12),\n",
       " (8, 1),\n",
       " (9, 2),\n",
       " (10, 15),\n",
       " (11, 21),\n",
       " (12, 10),\n",
       " (13, 8),\n",
       " (14, 16),\n",
       " (15, 0),\n",
       " (16, 6),\n",
       " (17, 5),\n",
       " (18, 20),\n",
       " (19, 18),\n",
       " (20, 11),\n",
       " (21, 14)}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a list of (tarot_card, idx), and sort it. This is kind of equivalent to np.argsort\n",
    "n_cards = len(deck1)\n",
    "sorted_deck1 = sorted((deck1[idx], idx) for idx in range(n_cards))  # O(N log N)\n",
    "sorted_deck2 = sorted((deck2[idx], idx) for idx in range(n_cards))  # O(N log N)\n",
    "\n",
    "matches = set()\n",
    "for idx in range(n_cards):  # O(N)\n",
    "    matches.add((sorted_deck1[idx][1], sorted_deck2[idx][1]))  # O(1)\n",
    "    \n",
    "matches"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5099970a",
   "metadata": {},
   "source": [
    "# 3. Fastest solution: O(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "509dda71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 13),\n",
       " (1, 9),\n",
       " (2, 3),\n",
       " (3, 19),\n",
       " (4, 7),\n",
       " (5, 17),\n",
       " (6, 4),\n",
       " (7, 12),\n",
       " (8, 1),\n",
       " (9, 2),\n",
       " (10, 15),\n",
       " (11, 21),\n",
       " (12, 10),\n",
       " (13, 8),\n",
       " (14, 16),\n",
       " (15, 0),\n",
       " (16, 6),\n",
       " (17, 5),\n",
       " (18, 20),\n",
       " (19, 18),\n",
       " (20, 11),\n",
       " (21, 14)}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a dictionary, mapping cards to the index in deck2\n",
    "deck2_card_to_idx = {}\n",
    "for idx2, card in enumerate(deck2):  # O(N)\n",
    "    deck2_card_to_idx[card] = idx2   # O(1)\n",
    "\n",
    "# For each card and index in deck1, look up the index in deck2, and store the match\n",
    "matches = set()\n",
    "for idx1, card in enumerate(deck1):  # O(N)\n",
    "    idx2 = deck2_card_to_idx[card]   # O(1)\n",
    "    matches.add((idx1, idx2))        # O(1)\n",
    "    \n",
    "matches"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "033a5674-faaa-4098-9f3d-c7cac46aab0f",
   "metadata": {},
   "source": [
    "# 3a. Fastest solution: O(N), different notation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c93f4606-ffef-431b-bdc1-ca5712e5aca0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 12),\n",
       " (1, 14),\n",
       " (2, 19),\n",
       " (3, 6),\n",
       " (4, 5),\n",
       " (5, 15),\n",
       " (6, 13),\n",
       " (7, 20),\n",
       " (8, 10),\n",
       " (9, 17),\n",
       " (10, 0),\n",
       " (11, 21),\n",
       " (12, 2),\n",
       " (13, 18),\n",
       " (14, 1),\n",
       " (15, 8),\n",
       " (16, 16),\n",
       " (17, 4),\n",
       " (18, 7),\n",
       " (19, 3),\n",
       " (20, 9),\n",
       " (21, 11)}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Same as above, using \"dict comprehension\"\n",
    "\n",
    "# Create a dictionary, mapping cards to the index in deck2\n",
    "deck2_card_to_idx = {card:idx2\n",
    "                     for idx2, card in enumerate(deck2)}  # O(N)\n",
    "\n",
    "# For each card and index in deck1, look up the index in deck2, and store the match\n",
    "matches = {(idx1, deck2_card_to_idx[card])      # O(1)\n",
    "           for idx1, card in enumerate(deck1)}  # O(N)\n",
    "\n",
    "matches"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
