{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Important reminder\n",
    "\n",
    "No lecture on 1 Nov. 2024 due to holiday.\n",
    "\n",
    "Lecture will be at start of tutorial on 4 Nov. 2024 (at the normal tutorial time and loaction, namely 14:15 in the Computer Pool in Bio 2/3 building.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e925a1ea56688b2e",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# types, variables, assignment, indexing, slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e21110f11e6d841a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# You must run this cell, but you can ignore its contents.\n",
    "\n",
    "import hashlib\n",
    "\n",
    "def ads_hash(ty):\n",
    "    \"\"\"Return a unique string for input\"\"\"\n",
    "    ty_str = str(ty).encode()\n",
    "    m = hashlib.sha256()\n",
    "    m.update(ty_str)\n",
    "    return m.hexdigest()[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-179bf0a88bde01d6",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Types\n",
    "\n",
    "Please answer the questions about Python types. Here we will use the spelling that Python uses for the types. Thus, we write `int` (not e.g. `integer`) and `str` (not e.g. `string`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-dffd2c4d1820c148",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "What is the type of the following:\n",
    "\n",
    "`10`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4c5bb2e24cd0b4b8",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a534d123d26a6e42",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='f918a1a5ca'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-02ed8f0df4c12fe7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "What is the type of the following:\n",
    "    \n",
    "`[1,2,3,4]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c53f7736f3ef6a4d",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-0997a02809ef51df",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='981a391f15'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-df6875a7f373d1e8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3\n",
    "\n",
    "What is the type of the following:\n",
    "    \n",
    "`(1,2,3,4)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-cecba2dc8f2a1fcb",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-d8e48e520e48ecbb",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='99e8d934d2'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q4\n",
    "\n",
    "What is the type of the following:\n",
    "    \n",
    "`\"(1,2,3,4)\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-01390d1edcadb200",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-81637082eb179284",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='17a0b2d655'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-3e29ab113c157d73",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q5\n",
    "\n",
    "What is the type of the following:\n",
    "    \n",
    "`'(1,2,3,4)'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d6436534e3fbc839",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-0e5bfe7fc5f7997b",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='17a0b2d655'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2e6d7d11312ed89d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Variables and assignment\n",
    "\n",
    "The first questions are a review."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-803f832c65529b6d",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    x = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9de8d3cdd252995f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-1ff60a7adb65c261",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='4a44dc1536'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-fc696d04bf395ee4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    x = 10\n",
    "    x = x+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-dae207003862512c",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-417322b120e1eab7",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='4fc82b26ae'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-91ffd31eabd4a539",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    y = [1,2,3]\n",
    "    x = y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bb17fad77ab0be6f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a58b1d4c175e7894",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='a36b1f2c3f'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-1ebdb5ef57dfd542",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4c3dd2ac7883f171",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    y = [1,2,3]\n",
    "    x = y[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f5a2887e76c4321e",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-5314eae929078d10",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='d4735e3a26'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-11f65d7c9b65371b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    x = [1,2,3]\n",
    "    x[2] = 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8f0317f086f58de7",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 42]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[1,2,42]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-5aa9f92a6e6efaa0",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='ac009e066e'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-31c1a47f78680df7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    x = [1,2,3]\n",
    "    y = x\n",
    "    y[1] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-196bc244f1c8042b",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 10, 3]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[1,10,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-39cc1a93f8204257",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='26796b1e40'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bc63d89f8ac73cc7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Slicing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-59f33c7f581a643f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    y = [1,2,3,4,5]\n",
    "    x = y[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c5984871c943365a",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-f9f8e930e5642969",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='5bf775530e'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5b06ff43b2d2b492",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    y = [1,2,3,4,5]\n",
    "    x = y[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7f8aa29eb2af0b7f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b9af5eba45805af2",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='a36b1f2c3f'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bcddf90d39f648f9",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3\n",
    "\n",
    "Consider the following code. After this has run, what will the value of `x` be?\n",
    "\n",
    "    y = [1,2,3,4,5]\n",
    "    x = y[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c921dc35e4b5e16e",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "[2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-1ba38ad0ab22e86e",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means the answer in your previous cell was correct.\n",
    "assert ads_hash(_)=='c4204dd36f'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ad8a134c4668a0d6",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Functions\n",
    "\n",
    "Theses questions should be a review for you"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-45775cc0e99b3744",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "Define a function called `my_function` which returns its input multiplied by three."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-450afdf17d3b0b19",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def my_function(x):\n",
    "    return x*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-22f30ed9a48dc320",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means `my_function` does indeed return three times its input.\n",
    "assert my_function(0)==0\n",
    "assert my_function(1)==3\n",
    "assert my_function(100)==300"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2ecaa6ef333bc779",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Here is a function definition for `a_times_b`, but this code has a problem. Fix the problem by defining a function `a_times_b` so that the tests below pass.\n",
    "\n",
    "```\n",
    "def a_times_b(a,b):\n",
    "    a*b\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7d6d492c969fd4cc",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def a_times_b(a,b):\n",
    "    return a*b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-739d96807909739e",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means `a_times_b` does indeed function as expected.\n",
    "assert a_times_b(0,10)==0\n",
    "assert a_times_b(10,10)==100\n",
    "assert a_times_b(10,0)==0\n",
    "assert a_times_b(6,7)==42\n",
    "assert a_times_b(7,6)==42"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c61877577f449834",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3\n",
    "\n",
    "Make a function named `append3` which assumes its input argument is a `list` and appends the number `3` to it. Your function should return `None`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-af763dc40964a8bd",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# Type your answer here and then run this and the following cell.\n",
    "def append3(x):\n",
    "    x.append(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-dfe77036dc289698",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "x = [1,4,10]\n",
    "assert ads_hash(append3(x))=='dc937b5989'\n",
    "assert ads_hash(x)=='c2a0352fa1'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2a1fc2dc6eef9997",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-dd7086db2c448b9f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# Below, we will use matplotlib, so we need to import it here.\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-44b659b622ef1e36",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "Given the following data, make a plot that looks like this:\n",
    "\n",
    "```\n",
    "x=[1,2,3,4,5,6,7,8,9,10]\n",
    "y=[0,4,0,3,3,0,3,4,5,2]\n",
    "```\n",
    "\n",
    "<img src=\"data:image/png;base64,[encoded data, 16568 characters]\" alt=\"\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-4283e6e240253fb1",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27360 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x=[1,2,3,4,5,6,7,8,9,10]\n",
    "y=[0,4,0,3,3,0,3,4,5,2]\n",
    "plt.plot(x,y);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0fbdb51502e1211a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Given the following data, make a plot that looks like this:\n",
    "\n",
    "```\n",
    "x=[1,2,3,4,5,6,7,8,9,10]\n",
    "y1=[0,4,0,3,3,0,3,4,5,2]\n",
    "y2=[3,2,4,4,2,4,4,2,4,2]\n",
    "```\n",
    "\n",
    "Hint, you can draw two lines by making two calls to `plt.plot()`.\n",
    "\n",
    "<img src=\"data:image/png;base64,[encoded data, 23804 characters]\" alt=\"\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-84eca66f88093703",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39348 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x=[1,2,3,4,5,6,7,8,9,10]\n",
    "y1=[0,4,0,3,3,0,3,4,5,2]\n",
    "y2=[3,2,4,4,2,4,4,2,4,2]\n",
    "plt.plot(x,y1)\n",
    "plt.plot(x,y2);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-911d3f2c6efedebc",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Scopes\n",
    "\n",
    "## Q1\n",
    "\n",
    "Fix the following code so that `myfunc` returns its input argument times `34`.\n",
    "\n",
    "```python\n",
    "x = 100\n",
    "\n",
    "def myfunc(a):\n",
    "    return x*34\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e62ac462a1466c64",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def myfunc(a):\n",
    "    return a*34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-66f596032f6fb1ae",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert ads_hash(myfunc(12))=='e6f47e008c'\n",
    "assert ads_hash(myfunc(123))=='ecdf40652a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9f94cfbb2410ebbe",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Modify the definition of the `func1` function so that it returns its input argument times `y` from the global scope. Keep the `y = 123` in your answer cell, but not in your function.\n",
    "\n",
    "```python\n",
    "y = 123\n",
    "\n",
    "def func1(x):\n",
    "    x*2\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f052e86277ac2b09",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "y = 123\n",
    "\n",
    "def func1(x):\n",
    "    return x*y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b6a2675dfc4862ea",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert ads_hash(func1(4))=='23e8b01758'\n",
    "assert ads_hash(func1(10))=='22a2fa7d04'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d92472e0e9ea07f6",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3 - Mixed function return values and \"side effects\"\n",
    "\n",
    "Consider this function:\n",
    "\n",
    "```python\n",
    "a = []\n",
    "def find_num(x,a):\n",
    "    a.append(x)\n",
    "    if x > 10:\n",
    "        return x/2\n",
    "    else:\n",
    "        return find_num(x+2,a)\n",
    "```\n",
    "\n",
    "Notice that each time the function runs, `a` in the global scope will get the value of `x` appended to it.\n",
    "\n",
    "How many times does this function run when started with `x=2`? Hint: you can check the length of the list `a` after it finishes running. To get the length of a list named `a`, use `len(a)`.\n",
    "\n",
    "Put your answer in the variable `n_runs`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-13834aebe49a3a45",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "a = []\n",
    "def find_num(x,a):\n",
    "    a.append(x)\n",
    "    if x > 10:\n",
    "        return x*16\n",
    "    else:\n",
    "        return find_num(x+2,a)\n",
    "    \n",
    "find_num(2,a)\n",
    "n_runs = len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-e350e97fb2289044",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert ads_hash(n_runs)=='e7f6c01177'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7d69975a16852789",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Fibonacci\n",
    "\n",
    "You may know the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number).\n",
    "\n",
    "```\n",
    "0 1 1 2 3 5 8 13\n",
    "```\n",
    "\n",
    "Notice that, after the first two numbers, each number is the sum of the previous two.\n",
    "\n",
    "Let's make a Python program to generate it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a797acc2cd4e44c3",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def exten_fib(a):\n",
    "    if len(a) < 2:\n",
    "        raise ValueError(\"not enough entries\")\n",
    "    num1 = a[-2]\n",
    "    num2 = a[-1]\n",
    "    a.append(num1+num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2042696ad549c125",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 1]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[0,1]\n",
    "exten_fib(a)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 1, 2, 3]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Modify this cell to explore calling `exten_fib` many times.\n",
    "\n",
    "a=[0,1]\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5770be94eba1bc42",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q1\n",
    "\n",
    "What is the next Fibonacci number after 987? Put your answer in the variable `after987`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e7674742e304e19f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]\n"
     ]
    }
   ],
   "source": [
    "a=[0,1]\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "exten_fib(a)\n",
    "print(a)\n",
    "\n",
    "after987 = 1597"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-0f5d26430311d09e",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "assert ads_hash(after987)=='eb57f94995'"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Create Assignment",
  "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
