{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-494826e8c27cdbcb",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# You must run this cell, but you can ignore its contents.\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": "code",
   "execution_count": 2,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-592b21fffec74d63",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# You must also run this cell.\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4e22e2660edab610",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Flow control - `for` loops with `range(n)`\n",
    "\n",
    "Remember from the previous exercise you defined a function like this:\n",
    "\n",
    "```python\n",
    "def simulate_generation(a,Rt):\n",
    "    previous = a[-1]\n",
    "    new_infected = previous*Rt\n",
    "    a.append(new_infected)\n",
    "```\n",
    "\n",
    "This implements a single new generation using the basic formula for reproduction:\n",
    "\n",
    "$N_{t+1} = R_t N_t$\n",
    "\n",
    "Now, consider this code:\n",
    "\n",
    "```python\n",
    "x = [1]\n",
    "for i in range(5):\n",
    "    simulate_generation(x, 2)\n",
    "```\n",
    "\n",
    "When run, `x` will have a value of `[1, 2, 4, 8, 16, 32]`.\n",
    "\n",
    "## Q1\n",
    "\n",
    "Write a function called `simulate_n_generations` which takes two arguments, `n` and `Rt`. The first argument `n` will be the number of generations to simulate. The second argument, `Rt` is as above. Your function should return the number of infected individuals, starting from a single individual, after `n` generations.\n",
    "\n",
    "The function signature should look like this:\n",
    "\n",
    "```python\n",
    "def simulate_n_generations(n,Rt):\n",
    "```\n",
    "\n",
    "If you run your new function like this:\n",
    "\n",
    "```python\n",
    "simulate_n_generations(10, 2)\n",
    "```\n",
    "\n",
    "the result should be `[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]`.\n",
    "\n",
    "Hint: copy the `simulate_generation` function definiton from above and use it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d2a294ee5e6f9e3b",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def simulate_generation(a,Rt):\n",
    "    previous = a[-1]\n",
    "    new_infected = previous*Rt\n",
    "    a.append(new_infected)\n",
    "    \n",
    "def simulate_n_generations(n,Rt):\n",
    "    a = [1]\n",
    "    for i in range(n):\n",
    "        simulate_generation(a,Rt)\n",
    "    return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-34471e259a21b377",
     "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",
    "\n",
    "assert ads_hash(simulate_n_generations(10, 2)) == 'a9ad958975'\n",
    "assert ads_hash(simulate_n_generations(9, 3)) == '1f0b039679'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7d95e6913ad535d7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q2\n",
    "\n",
    "Recall that \n",
    "\n",
    "- `range(n)` returns an *iterator* starting at `0` and ending at `n`.\n",
    "- `range(start, end)` returns an iterator starting at `start` and ending before `end`.\n",
    "- `range(start, end, inc)` returns an iterator starting at `start` and ending before `end` with an increment of `inc`.\n",
    "\n",
    "Thus, this code\n",
    "\n",
    "```python\n",
    "x = []\n",
    "for i in range(10, 1000, 203):\n",
    "    x.append(i)\n",
    "```\n",
    "\n",
    "Will set `x` equal to `[10, 213, 416, 619, 822]`\n",
    "\n",
    "Create a list named `my_list` which starts at 10, ends before 1000 and has an increment of 21."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-97f27abad4bca5a7",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "my_list = []\n",
    "for i in range(10, 1000, 21):\n",
    "    my_list.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-ba9f9e26ac285bb5",
     "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",
    "\n",
    "assert ads_hash(my_list)=='c25a617fa9'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-90de2683a98d0ac5",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Flow control - for loops that iterate through a list\n",
    "\n",
    "Remember that `for` loops will *iterate* through any *iterator*, and lists are iterators.\n",
    "\n",
    "Therefore this\n",
    "\n",
    "```python\n",
    "cumulative = 0\n",
    "for x in [1,2,3,100]:\n",
    "    cumulative = cumulative + x\n",
    "```\n",
    "\n",
    "sets `cumulative` to 106.\n",
    "\n",
    "## Q1\n",
    "\n",
    "Create a function `cum_sum` which takes a single argument `x`. `x` will be an iterator and your function should return the sum of all values in the iterator.\n",
    "\n",
    "The function signature is:\n",
    "\n",
    "```python\n",
    "def cum_sum(x):\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    },
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5c41a83f5efb3f97",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def cum_sum(x):\n",
    "    cum = 0\n",
    "    for i in x:\n",
    "        cum = cum + i\n",
    "    return cum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-01d1590cc0a3c37f",
     "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",
    "\n",
    "assert ads_hash(cum_sum([1,3,3,451,34,2,3]))=='dcb5d6e69e'\n",
    "assert ads_hash(cum_sum([1,3,3,-12,32,1,3]))=='eb1e33e8a8'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c6115c6a8fc2824f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Practice with functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6e6f3ca73e99b3e7",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "### Q1 Make a function `compute_seq2(n)` which computes the a sequence of length `n` according to the following pattern:\n",
    "\n",
    "`2, 2, 4, 6, 10, 16, 26 ...`\n",
    "\n",
    "Hint: this is a Fibonacci sequence with initial elements 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-8d48778fdbfb317e",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def compute_seq2(n):\n",
    "    if n == 1:\n",
    "        return [2]\n",
    "    elif n == 2:\n",
    "        return [2,2]\n",
    "    else:\n",
    "        result = compute_seq2(n-1)\n",
    "        a = result[-2]\n",
    "        b = result[-1]\n",
    "        result.append(a+b)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-e7e80f9bdb4eeb08",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means `compute_seq2()` behaves correctly.\n",
    "assert compute_seq2(16)==[2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220, 1974]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-2ce1883510b6a1b4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "### Q2 Make a function `compute_seq3(n)` which computes the a sequence of length `n` according to the following pattern:\n",
    "\n",
    "`1, 1, 1, 2, 3, 4, 6, 9, 13, 19, 28, ...`\n",
    "\n",
    "Hint: this is very similar to the Fibonacci sequence, but each element at index `n` is the sum of the elements at index `n-1` and `n-3`. The first three elements are `1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5aeb59f4e391239b",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def compute_seq3(n):\n",
    "    assert type(n)==int\n",
    "    assert n > 0\n",
    "    if n == 1:\n",
    "        return [1]\n",
    "    elif n == 2:\n",
    "        return [1,1]\n",
    "    elif n == 3:\n",
    "        return [1,1,1]\n",
    "    else:\n",
    "        result = compute_seq3(n-1)\n",
    "        a = result[-3]\n",
    "        b = result[-1]\n",
    "        result.append(a+b)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-0c52f8d0b74caa5e",
     "locked": true,
     "points": 1,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# If this runs without error, it means `compute_seq1()` behaves correctly.\n",
    "assert compute_seq3(16)==[1, 1, 1, 2, 3, 4, 6, 9, 13, 19, 28, 41, 60, 88, 129, 189]\n",
    "assert ads_hash(compute_seq3(30)) == '210bfe49ef'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-03d11f5c31865fa6",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Practice converting types and using loops\n",
    "\n",
    "You are a biologist at Cape Verde to study one of the most important nesting sites for loggerhead sea turtles. You are studying the numbers and sexes of young turtles as they hatch. During the data collection part of your project, you have taken notes by typing \"m\" for male and \"f\" for female."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-06a01be28c18339a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "# Here are your data.\n",
    "\n",
    "nest1 = \"fffffmmmfmmfffmfmmmfffffffmmffmmfffmmmmffmmffmmmfmmmffmmmmffffmmmfffmffmfmfmfmmffmfmmffmfmmf\"\n",
    "nest2 = \"mmmfmffffmmmfmmfmfmmffmffmfmfmffmffmmmfmfffmfmfffmffmmfmfmfmmffmfmmf\"\n",
    "nest3 = \"ffffffmmmfmmfffmfmmmffmfmmmfffmmfffmffffmffmmmffffffmmmfmmfffmfmmmffmfmm\"\n",
    "nest4 = \"fmfmfmmffmffmfmffffffmfmfmffmffmfmfmmfffffmfmfmmffmffmfmmmfmmmmmmfmfmmm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-c998e2a3545270ce",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "A string (`str` in Python) is a *sequence* which can be *iterated*, as in a for loop. You also know the `range` and `list` types, which can also be iterated. When you iterate over an `str`, each item is a signle character `str`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7f96e853d095f8fc",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "count = 0\n",
    "for char in nest1:\n",
    "    assert type(char)==str\n",
    "    count = count + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-23e2ec7eac2011ae",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "92"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e874a549c51ed6e9",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q3 How many male turtles are in nest1? Count the number of `\"m\"` characters are in the `nest` string. Put the answer in a variable `num_males`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-efbc7c6cbe040063",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "num_males = 0\n",
    "for char in nest1:\n",
    "    assert type(char)==str\n",
    "    if char=='m':\n",
    "        num_males = num_males + 1\n",
    "    else:\n",
    "        assert char=='f'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-62ab53f78c7a1aa4",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_males"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-f6cf427c0e834b6d",
     "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(num_males)=='71ee45a3c0'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Q4 What fraction of all turtles from nest1 are male? Put the answer in a variable `sex_ratio`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-545e9eaa06507765",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "sex_ratio=num_males / len(nest1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-aec1f9ca60c05951",
     "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(round(sex_ratio*1000))=='200dd69b70'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9a788ebd56edca64",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q5 Create an empty dict called `empty_observations` with keys 'nest1', 'nest2', 'nest3', 'nest4'. Each value should be an empty list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-cc33b612e98cae30",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "empty_observations = {'nest1':[],'nest2':[],'nest3':[],'nest4':[]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-b24ea76aaa1c504a",
     "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(tuple([item for item in empty_observations.items()]))=='5c604a4cc5'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-73e0ee5363629b1a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q6 Create a dict called `observations` with keys 'nest1', 'nest2', 'nest3', 'nest4'. The values for each key should be as given above in the section just after \"Here are your data.\"\n",
    "\n",
    "The key 'nest1' should have value given above for the `nest1` variable (i.e. `\"fffffmmmfmmfffmfmmmfffffffmmffmmfffmmmmffmmffmmmfmmmffmmmmffffmmmfffmffmfmfmfmmffmfmmffmfmmf\"`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-40c757b8f13d8839",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "observations = {'nest1': \"fffffmmmfmmfffmfmmmfffffffmmffmmfffmmmmffmmffmmmfmmmffmmmmffffmmmfffmffmfmfmfmmffmfmmffmfmmf\",\n",
    "'nest2': \"mmmfmffffmmmfmmfmfmmffmffmfmfmffmffmmmfmfffmfmfffmffmmfmfmfmmffmfmmf\",\n",
    "'nest3': \"ffffffmmmfmmfffmfmmmffmfmmmfffmmfffmffffmffmmmffffffmmmfmmfffmfmmmffmfmm\",\n",
    "'nest4': \"fmfmfmmffmffmfmffffffmfmfmffmffmfmfmmfffffmfmfmmffmffmfmmmfmmmmmmfmfmmm\",}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-41699e4baeb4f512",
     "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(tuple([item for item in observations.items()]))=='19ea06edbc'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d2bdc242834633ea",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Iterating through a `dict`\n",
    "\n",
    "Consider the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-57062ecaa6dd4e24",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "data = {'a': 1, 'b': 2, 'c': 3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-1a4e6fb36a964a4c",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key: a, value: 1\n",
      "key: b, value: 2\n",
      "key: c, value: 3\n"
     ]
    }
   ],
   "source": [
    "for key in data:\n",
    "    value = data[key]\n",
    "    print(f\"key: {key}, value: {value}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7a45e8eeafa0152b",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "We see in the above that we can iterate through this dictionary. Let's create a new dictionary based on the first one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-154ab8a613fe8638",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 4, 'c': 9}\n"
     ]
    }
   ],
   "source": [
    "squared_data = {}\n",
    "for key in data:\n",
    "    value = data[key]\n",
    "    squared_data[key] = value*value\n",
    "print(squared_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-55d5e2b1c6ca9071",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q7. Putting things together: create a dict called `sex_ratios`.\n",
    "\n",
    "There should be key for every key from the `observations` dictionary and the value for each key should be the sex ratio for each value in the `observations` dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d890f019a87700d7",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "def calc_sex_ratio(x):\n",
    "    num_males = 0\n",
    "    for c in x:\n",
    "        if c == 'm':\n",
    "            num_males = num_males + 1\n",
    "        else:\n",
    "            assert c=='f'\n",
    "    return num_males/len(x)\n",
    "\n",
    "sex_ratios = {}\n",
    "for nest in observations:\n",
    "    sex_ratios[nest] = calc_sex_ratio(observations[nest])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'nest1': 0.4782608695652174,\n",
       " 'nest2': 0.4852941176470588,\n",
       " 'nest3': 0.4444444444444444,\n",
       " 'nest4': 0.4788732394366197}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_ratios"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-a583495a57a86cfe",
     "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(tuple([(k,round(v*10000)) for (k,v) in sex_ratios.items()]))=='9d19a5431a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-839a79cbcdf3f3cf",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "## Q8. Plotting data\n",
    "\n",
    "Given the following data, make a plot that looks like the following:\n",
    "\n",
    "<img src=\"data:image/png;base64,[encoded data, 13716 characters]\" alt=\"\" />\n",
    "\n",
    "```\n",
    "year= [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015]\n",
    "sex_ratio = [0.51, 0.502, 0.49, 0.495, 0.503, 0.498, 0.51, 0.502, 0.49, 0.495, 0.503, 0.498, 0.73, 0.80]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "nbgrader": {
     "grade": true,
     "grade_id": "cell-369c41d4c436ea90",
     "locked": false,
     "points": 1,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22972 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "year= [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015]\n",
    "sex_ratio = [0.51, 0.502, 0.49, 0.495, 0.503, 0.498, 0.51, 0.502, 0.49, 0.495, 0.503, 0.498, 0.73, 0.80]\n",
    "plt.plot(year,sex_ratio,'o-');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e3d96d589e391631",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "# Read more about this topic\n",
    "\n",
    "While the above \"data\" in this notebook are completely fake, it was inspired by the paper [here](https://www.int-res.com/abstracts/meps/v621/p209-219), which was covered in the press [here](https://www.washingtonpost.com/world/africa/the-warming-climate-is-turning-baby-sea-turtles-one-gender/2019/10/21/d571f3fe-e3a6-11e9-b0a6-3d03721b85ef_story.html)."
   ]
  }
 ],
 "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
}
