{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "deletable": false,
    "editable": false,
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# Initialization cell\n",
    "try:  # for CS1302 JupyterLite pyodide kernel\n",
    "    import piplite\n",
    "\n",
    "    with open(\"requirements.txt\") as f:\n",
    "        for package in f:\n",
    "            package = package.strip()\n",
    "            print(\"Installing\", package)\n",
    "            await piplite.install(package)\n",
    "except ModuleNotFoundError:\n",
    "    pass\n",
    "\n",
    "import jupytext\n",
    "import otter\n",
    "from collections import namedtuple  # for naming the cards\n",
    "from random import choice           # for shuffling of the cards\n",
    "from pandas import DataFrame        # for creating tables\n",
    "\n",
    "grader = otter.Notebook(\"main.ipynb\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# Lab 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    },
    "tags": [
     "remove-cell"
    ]
   },
   "source": [
    "**CS1302 Introduction to Computer Programming**\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following cell first to initialize the environment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "## Card guessing game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Game rules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Consider a deck of 52 cards:\n",
    "<table>\n",
    "    <tr>\n",
    "        <td></td>\n",
    "        <th>1 (A)</th>\n",
    "        <th>2</th>\n",
    "        <th>3</th>\n",
    "        <th>4</th>\n",
    "        <th>5</th>\n",
    "        <th>6</th>\n",
    "        <th>7</th>\n",
    "        <th>8</th>\n",
    "        <th>9</th>\n",
    "        <th>10</th>\n",
    "        <th>11 (J)</th>\n",
    "        <th>12 (Q)</th>\n",
    "        <th>13 (K)</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <th style=\"transform: rotate(-90deg);\">Diamond</th>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-A-Diamond.svg\"><img width=\"50\" alt=\"Cards-A-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/e9/Cards-A-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-2-Diamond.svg\"><img width=\"50\" alt=\"Cards-2-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/99/Cards-2-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-3-Diamond.svg\"><img width=\"50\" alt=\"Cards-3-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/4/44/Cards-3-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-4-Diamond.svg\"><img width=\"50\" alt=\"Cards-4-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/a/af/Cards-4-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-5-Diamond.svg\"><img width=\"50\" alt=\"Cards-5-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/dd/Cards-5-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-6-Diamond.svg\"><img width=\"50\" alt=\"Cards-6-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/4/44/Cards-6-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-7-Diamond.svg\"><img width=\"50\" alt=\"Cards-7-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/2/2b/Cards-7-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-8-Diamond.svg\"><img width=\"50\" alt=\"Cards-8-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/90/Cards-8-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-9-Diamond.svg\"><img width=\"50\" alt=\"Cards-9-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/2/25/Cards-9-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-10-Diamond.svg\"><img width=\"50\" alt=\"Cards-10-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c2/Cards-10-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-J-Diamond.svg\"><img width=\"50\" alt=\"Cards-J-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/78/Cards-J-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-Q-Diamond.svg\"><img width=\"50\" alt=\"Cards-Q-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c3/Cards-Q-Diamond.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-K-Diamond.svg\"><img width=\"50\" alt=\"Cards-K-Diamond\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/55/Cards-K-Diamond.svg\"></a></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <th style=\"transform: rotate(-90deg);\">Club</th>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-A-Club.svg\"><img width=\"50\" alt=\"Cards-A-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c4/Cards-A-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-2-Club.svg\"><img width=\"50\" alt=\"Cards-2-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/b/b0/Cards-2-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-3-Club.svg\"><img width=\"50\" alt=\"Cards-3-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/e0/Cards-3-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-4-Club.svg\"><img width=\"50\" alt=\"Cards-4-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/6/69/Cards-4-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-5-Club.svg\"><img width=\"50\" alt=\"Cards-5-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7e/Cards-5-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-6-Club.svg\"><img width=\"50\" alt=\"Cards-6-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/a/af/Cards-6-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-7-Club.svg\"><img width=\"50\" alt=\"Cards-7-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/8/8e/Cards-7-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-8-Club.svg\"><img width=\"50\" alt=\"Cards-8-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/f/fd/Cards-8-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-9-Club.svg\"><img width=\"50\" alt=\"Cards-9-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/a/ac/Cards-9-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-10-Club.svg\"><img width=\"50\" alt=\"Cards-10-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/2/25/Cards-10-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-J-Club.svg\"><img width=\"50\" alt=\"Cards-J-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c7/Cards-J-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-Q-Club.svg\"><img width=\"50\" alt=\"Cards-Q-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/3/37/Cards-Q-Club.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-K-Club.svg\"><img width=\"50\" alt=\"Cards-K-Club\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/9e/Cards-K-Club.svg\"></a></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <th style=\"transform: rotate(-90deg);\">Heart</th>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-A-Heart.svg\"><img width=\"50\" alt=\"Cards-A-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/6/60/Cards-A-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-2-Heart.svg\"><img width=\"50\" alt=\"Cards-2-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/6/6e/Cards-2-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-3-Heart.svg\"><img width=\"50\" alt=\"Cards-3-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/57/Cards-3-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-4-Heart.svg\"><img width=\"50\" alt=\"Cards-4-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/3/39/Cards-4-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-5-Heart.svg\"><img width=\"50\" alt=\"Cards-5-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/91/Cards-5-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-6-Heart.svg\"><img width=\"50\" alt=\"Cards-6-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/55/Cards-6-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-7-Heart.svg\"><img width=\"50\" alt=\"Cards-7-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/d4/Cards-7-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-8-Heart.svg\"><img width=\"50\" alt=\"Cards-8-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/55/Cards-8-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-9-Heart.svg\"><img width=\"50\" alt=\"Cards-9-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/d2/Cards-9-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-10-Heart.svg\"><img width=\"50\" alt=\"Cards-10-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/76/Cards-10-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-J-Heart.svg\"><img width=\"50\" alt=\"Cards-J-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/e7/Cards-J-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-Q-Heart.svg\"><img width=\"50\" alt=\"Cards-Q-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/2/28/Cards-Q-Heart.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-K-Heart.svg\"><img width=\"50\" alt=\"Cards-K-Heart\" src=\"https://upload.wikimedia.org/wikipedia/commons/b/bf/Cards-K-Heart.svg\"></a></td>\n",
    "    </tr>        \n",
    "    <tr>\n",
    "        <th style=\"transform: rotate(-90deg);\">Spade</th>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-A-Spade.svg\"><img width=\"50\" alt=\"Cards-A-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/9d/Cards-A-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-2-Spade.svg\"><img width=\"50\" alt=\"Cards-2-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/e7/Cards-2-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-3-Spade.svg\"><img width=\"50\" alt=\"Cards-3-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/d0/Cards-3-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-4-Spade.svg\"><img width=\"50\" alt=\"Cards-4-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/4/4e/Cards-4-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-5-Spade.svg\"><img width=\"50\" alt=\"Cards-5-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/b/b1/Cards-5-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-6-Spade.svg\"><img width=\"50\" alt=\"Cards-6-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/6/68/Cards-6-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-7-Spade.svg\"><img width=\"50\" alt=\"Cards-7-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/c/c6/Cards-7-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-8-Spade.svg\"><img width=\"50\" alt=\"Cards-8-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7e/Cards-8-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-9-Spade.svg\"><img width=\"50\" alt=\"Cards-9-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/0/0a/Cards-9-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-10-Spade.svg\"><img width=\"50\" alt=\"Cards-10-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/6/67/Cards-10-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-J-Spade.svg\"><img width=\"50\" alt=\"Cards-J-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/ea/Cards-J-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-Q-Spade.svg\"><img width=\"50\" alt=\"Cards-Q-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/e/ef/Cards-Q-Spade.svg\"></a></td>\n",
    "        <td><a title=\"GW Simulations / Public domain\" href=\"https://commons.wikimedia.org/wiki/File:Cards-K-Spade.svg\"><img width=\"50\" alt=\"Cards-K-Spade\" src=\"https://upload.wikimedia.org/wikipedia/commons/1/18/Cards-K-Spade.svg\"></a></td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "-   Each card is in one of the four suits:  \n",
    "  *Diamond*, *Club*, *Heart*, and *Spade*.\n",
    "-   Each card has a value ordered as follows:  \n",
    "  $1 \\text{ (A)} < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 \\text{ (J)} < 12 \\text{ (Q)} < 13 \\text{ (K)}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{admonition} Rules\n",
    "\n",
    "![Card guessing game](images/card_game.dio.svg)\n",
    "\n",
    "- A dealer randomly picks a card without letting the player know.\n",
    "- The player needs to guess what exactly that card.\n",
    "- The player can make an informed guess after \n",
    "  - raising one-by-one *at most 6 yes/no questions*, to which\n",
    "  - the dealer must *answer immediately and honestly* after every question.\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```{attention}\n",
    "The player cannot ask:  \n",
    "- What is the value?\n",
    "- What is the suite?\n",
    "\n",
    "because the answers are not **yes/no**.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "For instance, the player may ask:\n",
    "- Is the suit club?\n",
    "- Is the card diamond 1 (A)?\n",
    "- Is the value at least 10?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One strategy is to ask whether the randomly drawn card is precisely certain card such as:\n",
    "> Is it Diamond 1 (A)?\n",
    "\n",
    "If yes, the player simply guess Diamond Ace and wins. If not, the player ask whether the randomly drawn card is another card such as:  \n",
    "> Is it Diamond 2? \n",
    "\n",
    "The player keeps asking until the answer is yes or he cannot ask any further."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise** \n",
    "\n",
    "Assign to the variable `chance` the chance of winning as a [floating point number](https://docs.python.org/3/tutorial/floatingpoint.html) between $0$ and $1$ in the following code cell. Your answer should be accurate at least up to 2 decimal places."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "chance",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "slideshow": {
     "slide_type": "-"
    },
    "tags": [
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "chance = 0.01923076923"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "deletable": false,
    "editable": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p><strong><pre style='display: inline;'>chance</pre></strong> passed!</p>"
      ],
      "text/plain": [
       "chance results: All test cases passed!"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grader.check(\"chance\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````{caution}\n",
    "Passing the test above means that you passes the visible test,\n",
    "\n",
    "```python\n",
    "assert isinstance(chance, float) \n",
    "assert chance >= 0\n",
    "assert chance <= 1\n",
    "```\n",
    "\n",
    "which only checks whether `chance` is\n",
    "\n",
    "- a floating number\n",
    "- $\\geq 0$ and\n",
    "- $\\leq 1$.\n",
    "\n",
    "Passing the visible test *does not mean* your answer is correct. You answer will checked by a hidden test.\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Coding the game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**How to create a deck of cards in python?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Run the following code to create a deck of cards."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-20T14:23:40.439389Z",
     "start_time": "2021-03-20T14:23:40.434613Z"
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": "-"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Card(value=1, suit='Diamond'),\n",
       " Card(value=1, suit='Club'),\n",
       " Card(value=1, suit='Heart'),\n",
       " Card(value=1, suit='Spade'),\n",
       " Card(value=2, suit='Diamond'),\n",
       " Card(value=2, suit='Club'),\n",
       " Card(value=2, suit='Heart'),\n",
       " Card(value=2, suit='Spade'),\n",
       " Card(value=3, suit='Diamond'),\n",
       " Card(value=3, suit='Club'),\n",
       " Card(value=3, suit='Heart'),\n",
       " Card(value=3, suit='Spade'),\n",
       " Card(value=4, suit='Diamond'),\n",
       " Card(value=4, suit='Club'),\n",
       " Card(value=4, suit='Heart'),\n",
       " Card(value=4, suit='Spade'),\n",
       " Card(value=5, suit='Diamond'),\n",
       " Card(value=5, suit='Club'),\n",
       " Card(value=5, suit='Heart'),\n",
       " Card(value=5, suit='Spade'),\n",
       " Card(value=6, suit='Diamond'),\n",
       " Card(value=6, suit='Club'),\n",
       " Card(value=6, suit='Heart'),\n",
       " Card(value=6, suit='Spade'),\n",
       " Card(value=7, suit='Diamond'),\n",
       " Card(value=7, suit='Club'),\n",
       " Card(value=7, suit='Heart'),\n",
       " Card(value=7, suit='Spade'),\n",
       " Card(value=8, suit='Diamond'),\n",
       " Card(value=8, suit='Club'),\n",
       " Card(value=8, suit='Heart'),\n",
       " Card(value=8, suit='Spade'),\n",
       " Card(value=9, suit='Diamond'),\n",
       " Card(value=9, suit='Club'),\n",
       " Card(value=9, suit='Heart'),\n",
       " Card(value=9, suit='Spade'),\n",
       " Card(value=10, suit='Diamond'),\n",
       " Card(value=10, suit='Club'),\n",
       " Card(value=10, suit='Heart'),\n",
       " Card(value=10, suit='Spade'),\n",
       " Card(value=11, suit='Diamond'),\n",
       " Card(value=11, suit='Club'),\n",
       " Card(value=11, suit='Heart'),\n",
       " Card(value=11, suit='Spade'),\n",
       " Card(value=12, suit='Diamond'),\n",
       " Card(value=12, suit='Club'),\n",
       " Card(value=12, suit='Heart'),\n",
       " Card(value=12, suit='Spade'),\n",
       " Card(value=13, suit='Diamond'),\n",
       " Card(value=13, suit='Club'),\n",
       " Card(value=13, suit='Heart'),\n",
       " Card(value=13, suit='Spade')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a deck of cards\n",
    "suits = (\"Diamond\", \"Club\", \"Heart\", \"Spade\")\n",
    "values = range(1, 14)\n",
    "Card = namedtuple(\"Card\", [\"value\", \"suit\"])  # namedtuple from collections\n",
    "\n",
    "deck = [Card(value, suit) for value in values for suit in suits]\n",
    "deck  # output the deck of cards to the output cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{note}\n",
    "The above code uses tuples and lists, which are composite data types that will be introduced later in the course.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**How to randomly pick a card in python?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following code cell multiple times to draw cards randomly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-20T14:23:44.399466Z",
     "start_time": "2021-03-20T14:23:44.397181Z"
    },
    "slideshow": {
     "slide_type": "-"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Card(value=5, suit='Diamond')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choice(deck)  # random imported before"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "```{note}\n",
    "For the computer to choose a card from the deck randomly, it uses a *(pseudo-)random number generator*, which will be introduced in more details later in the course.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "**How to implement a computer player?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given you have randomly draw a card,\n",
    "\n",
    "1. run the following code cell, and\n",
    "2. answer the 6 questions raised by the player honestly by entering  \n",
    "    - `y` for yes;\n",
    "    - `n` for no."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-20T14:23:58.379216Z",
     "start_time": "2021-03-20T14:23:47.863209Z"
    },
    "slideshow": {
     "slide_type": "-"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Is the suite either heart or spade? (y/[n])  n\n",
      "Is the suite either club or spade? (y/[n])  n\n",
      "Is the number 8 or above? (y/[n])  n\n",
      "Is the number 4 or above? (y/[n])  y\n",
      "Is the number 6 or above? (y/[n])  n\n",
      "Is the number 5 or above? (y/[n])  y\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The card is Diamond 5\n"
     ]
    }
   ],
   "source": [
    "suit_idx = 0\n",
    "number = 0\n",
    "if \"y\" == input(\"Is the suite either heart or spade? (y/[n]) \").lower():\n",
    "    suit_idx += 2\n",
    "if \"y\" == input(\"Is the suite either club or spade? (y/[n]) \").lower():\n",
    "    suit_idx += 1\n",
    "if \"y\" == input(f\"Is the number {number+8} or above? (y/[n]) \").lower():\n",
    "    number += 8\n",
    "if \"y\" == input(f\"Is the number {number+4} or above? (y/[n]) \").lower():\n",
    "    number += 4\n",
    "if \"y\" == input(f\"Is the number {number+2} or above? (y/[n]) \").lower():\n",
    "    number += 2\n",
    "if \"y\" == input(f\"Is the number {number+1} or above? (y/[n]) \").lower():\n",
    "    number += 1\n",
    "print(f\"The card is {suits[suit_idx]} {number}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{note}\n",
    "To calculate the guess based on the answers to the yes/no questions, the above code uses *conditional statements*, which will be covered very soon in the course.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The computer program always win because: \n",
    "\n",
    "- Each question is asked so that the set of remaining possibilities is split into two groups of sizes as close as possible. \n",
    "- $6$ questions can therefore resolve $2^6=64>52$ possibilities, leading to singleton groups.\n",
    "\n",
    "In other words, each card is associated with a unique sequence of $6$ Yes/No answers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**Exercise** \n",
    "\n",
    "Is it possible to program a player to always win with $5$ questions?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No. 5 questions can only resolve 32 possibilities."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Representing non-negative integers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "The following table gives the binary representions of unsigned decimal integers from 0 to 7."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Binary | Decimal |\n",
    "|:------:|:-------:|\n",
    "|  000   |    0    |\n",
    "|  001   |    1    |\n",
    "|  010   |    2    |\n",
    "|  011   |    3    |\n",
    "|  100   |    4    |\n",
    "|  101   |    5    |\n",
    "|  110   |    6    |\n",
    "|  111   |    7    |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Observe that the binary representations of 4, 5, 6, and 7 all have **1** in the leftmost (most significant) bit:\n",
    "\n",
    "| Binary  | Decimal |\n",
    "|:-------:|:-------:|\n",
    "|   000   |    0    |\n",
    "|   001   |    1    |\n",
    "|   010   |    2    |\n",
    "|   011   |    3    |\n",
    "| **1**00 |  **4**  |\n",
    "| **1**01 |  **5**  |\n",
    "| **1**10 |  **6**  |\n",
    "| **1**11 |  **7**  |\n",
    "\n",
    "Consider that bit as the answer to the following **yes/no** question:\n",
    "\n",
    "> Is the integer 4 or above?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Now, to convert the entire binary sequence to decimal, we can think of the conversion as a guessing game where\n",
    "- the binary sequence is a sequence of **yes (1)** or **no (0)** answers to certain **yes/no** questions, and\n",
    "- the informed guess is the integer represented by the binary sequence.\n",
    "\n",
    "![Decoding tree](images/dt.dio.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "**Exercise** \n",
    "\n",
    "What are the **yes/no** questions (Q2 and Q3 above) corresponding to the 2nd bit and 3rd bit?\n",
    "\n",
    "```{hint}\n",
    "\n",
    "See\n",
    "- [Binary Number System](https://www.mathsisfun.com/binary-number-system.html) and\n",
    "- [Binary Number Conversions](https://www.purplemath.com/modules/numbbase.htm).\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "is the integer (maximum number under previous conditions) or above?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Representing negative integers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "The following also uses 3 bits to represent 8 numbers:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "| Binary  | Decimal |\n",
    "|:-------:|:-------:|\n",
    "|   000   |    0    |\n",
    "|   001   |    1    |\n",
    "|   010   |    2    |\n",
    "|   011   |    3    |\n",
    "| **1**00 | **-4**  |\n",
    "| **1**01 | **-3**  |\n",
    "| **1**10 | **-2**  |\n",
    "| **1**11 | **-1**  |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```{note}\n",
    "\n",
    "- The numbers 0, 1, 2, and 3 have the same binary representations as before, but\n",
    "- the binary representations for 4, 5, 6, and 7 are now used for -4, -3, -2, and -1. \n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "**What is the benefit of the above representation?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "To subtract a positive binary number $x$ from another positive binary number $y$, i.e.,\n",
    "\n",
    "$$x - y,$$\n",
    "\n",
    "it seems we can instead perform the binary addition\n",
    "\n",
    "$$x + (-y)$$\n",
    "\n",
    "of a positive binary number $x$ and a negative binary number $-y$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "For example,\n",
    "\n",
    "$$\n",
    "\\overbrace{011_2}^{3} + \\overbrace{100_2}^{-4} = \\underbrace{111_2}_{-1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "It seem to work as well if there are bits carried forward, e.g., $1 + (- 3)$ in binary is\n",
    "\n",
    "$$\n",
    "\\begin{array}{rrrr}\n",
    " & 0 & \\overset{\\color{blue} 1}0 & 1 \\\\\n",
    "+ & 1 & 0 & 1 \\\\\\hline\n",
    "& 1 & 1 &  0 \n",
    "\\end{array}\n",
    "$$\n",
    "\n",
    "which translate to $-2$ in decimal as desired."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "**Exercise** \n",
    "\n",
    "There is a subtlety when computing $3 - 2$ using binary addition because \n",
    "\n",
    "$$ \\overbrace{011_2}^3 + \\overbrace{110_2}^{-2} = 1001_2$$\n",
    "\n",
    "which overflows to $4$ bits, a seemingly invalid binary representation. Fortunately, there is a simple fix so binary addition can still apply. Come up with such a fix that also works for other cases such as $3-3$, $2-1$, etc.\n",
    "\n",
    "```{hint}\n",
    "See [two's complement represenation](https://en.wikipedia.org/wiki/Two%27s_complement).\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ignore the first bit and only consider the last 3 bits."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building a computer from transistors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A computer is built from transistors that operates on binary states \n",
    "\n",
    "> *on/off*, \n",
    "\n",
    "which is also represented as bits \n",
    "\n",
    "> *1/0*\n",
    "\n",
    "or boolean values \n",
    "\n",
    "> *True/False*."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the addition of $2$ bits $A$ and $B$:\n",
    "\n",
    "$$\n",
    "A + B = C\\circ S\n",
    "$$\n",
    "\n",
    "\n",
    "| A | B | C$\\circ$S |\n",
    "|:---:|:---:|:----------:|\n",
    "|  0  |  0  |     00     |\n",
    "|  0  |  1  |     01     |\n",
    "|  1  |  0  |     01     |\n",
    "|  1  |  1  |     10     |\n",
    "\n",
    "where $C\\circ S$ is just the concatenation of two bits $C$ and $S$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The transistors, called *logic gates*, can carry out basic logic operations such as\n",
    "\n",
    "- $A \\operatorname{AND} B$: which returns $1$ if and only if both $A$ and $B$ are $1$, and $0$ otherwise.\n",
    "- $A \\operatorname{XOR} B$: which returns $1$ if and only if either $A$ and $B$ are $1$ but not both.\n",
    "\n",
    "The complete input and output relationships are listed below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>A AND B</th>\n",
       "      <th>A XOR B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  A AND B  A XOR B\n",
       "0  0  0        0        0\n",
       "1  0  1        0        1\n",
       "2  1  0        0        1\n",
       "3  1  1        1        0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataFrame(\n",
    "    [[A, B, A & B, A ^ B] for A in (0, 1) for B in (0, 1)],\n",
    "    columns=[\"A\", \"B\", \"A AND B\", \"A XOR B\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{note}\n",
    "The logical functions are computed using bitwise operators `&` and `^`, and the result is stored as a `DataFrame` object, which will be explained later in the course.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise**\n",
    "\n",
    "What is the logic in computing $C$ and $S$ from $A$ and $B$? Use the logic gates AND and XOR only."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "adder",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "source": [
    "```{hint}\n",
    "See the logic gate simulator:\n",
    "\n",
    "1. Visit https://logic.ly/demo/\n",
    "2. Select *1-Bit Full Adder*.\n",
    "3. Change the input and observe the output.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>CS</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  CS\n",
       "0  0  0   0\n",
       "1  0  1   1\n",
       "2  1  0   1\n",
       "3  1  1   1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataFrame(\n",
    "    [[A, B, A & B+ A ^ B] for A in (0, 1) for B in (0, 1)],\n",
    "    columns=[\"A\", \"B\", \"CS\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{seealso}\n",
    "If you play minecraft, you can build the following devices based on how computers are built from logic gates:\n",
    "\n",
    "- an adder for binary addition: https://youtu.be/vtpTri-PZXY\n",
    "- a calculator for calculus: https://youtu.be/5NzIt9sns6o\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "editable": false,
    "tags": [
     "remove-cell"
    ]
   },
   "source": [
    "## Submission\n",
    "\n",
    "Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. **Please save before exporting!**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# extra files to submit\n",
    "extra_files = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": false,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Generate the source main.py necessary for grading and similarity check.\n",
    "jupytext.write(jupytext.read(\"main.ipynb\"), \"main.py\", fmt=\"py:percent\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Generate the zip file to submit.\n",
    "grader.export(pdf=False, run_tests=False, files=[\"main.py\", *extra_files])"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3.10 (XPython)",
   "language": "python",
   "name": "xpython"
  },
  "language_info": {
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "version": "3.10.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "otter": {
   "OK_FORMAT": true,
   "tests": {
    "chance": {
     "name": "chance",
     "points": null,
     "suites": [
      {
       "cases": [
        {
         "code": ">>> assert isinstance(chance, float) \n>>> assert chance >= 0\n>>> assert chance <= 1\n",
         "hidden": false,
         "locked": false
        }
       ],
       "scored": true,
       "setup": "",
       "teardown": "",
       "type": "doctest"
      }
     ]
    }
   }
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
