696 lines
57 KiB
Plaintext
696 lines
57 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Reminder: Do not rename this file (or allow your computer to do that)\n",
|
||
|
"\n",
|
||
|
"Please overwrite the original exercise release file(s) when uploading your assignment. I still had to fix some of your submissions last week."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-f2aebf59df557e79",
|
||
|
"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-5b7440fcb507ff19",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# Flow control - if statements\n",
|
||
|
"\n",
|
||
|
"## Q1\n",
|
||
|
"\n",
|
||
|
"Write a function called `is_over10` which accepts a single input argument and which returns the boolean `True` if the input is greater than 10. Otherwise the function should return `False`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-dd1841337da413fe",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def is_over10(x):\n",
|
||
|
" if x > 10:\n",
|
||
|
" return True\n",
|
||
|
" else:\n",
|
||
|
" return False"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-29a08b5273e7cfd5",
|
||
|
"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(is_over10(9)) == '60a33e6cf5'\n",
|
||
|
"assert ads_hash(is_over10(9.9999)) == '60a33e6cf5'\n",
|
||
|
"assert ads_hash(is_over10(11)) == '3cbc87c768'\n",
|
||
|
"assert ads_hash(is_over10(10.000001)) == '3cbc87c768'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-24766e51350b14a2",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q2\n",
|
||
|
"\n",
|
||
|
"Write a function called `is_over_a` which accepts two input arguments `x` (the first argument) and `a` (the second argument). The function should return the boolean `True` if `x` is greater to or equal than `a`. Otherwise the function should return `False`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-fbff7f837b8a992a",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def is_over_a(x,a):\n",
|
||
|
" if x >= a:\n",
|
||
|
" return True\n",
|
||
|
" else:\n",
|
||
|
" return False"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-eacb9f0672c5988e",
|
||
|
"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(is_over_a(9,10)) == '60a33e6cf5'\n",
|
||
|
"assert ads_hash(is_over_a(9.9999,10)) == '60a33e6cf5'\n",
|
||
|
"assert ads_hash(is_over_a(10,10)) == '3cbc87c768'\n",
|
||
|
"assert ads_hash(is_over_a(11,10)) == '3cbc87c768'\n",
|
||
|
"assert ads_hash(is_over_a(10.000001,10)) == '3cbc87c768'\n",
|
||
|
"assert ads_hash(is_over_a(-9,-10)) == '3cbc87c768'\n",
|
||
|
"assert ads_hash(is_over_a(-9,10)) == '60a33e6cf5'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-53136179efb5686e",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# Flow control - `while` loops using $R_t$, the *effective reproduction number*\n",
|
||
|
"\n",
|
||
|
"To make our examples more interesting, we are going to use some concepts from epidemiology. We will perform some simple epidemiological modeling of disease spread. First, let's define $R_t$, the [*effective reproduction number*](https://en.wikipedia.org/wiki/Basic_reproduction_number#Effective_reproduction_number). This is the the average number of new infections caused by a single infected individual at time $t$ in a partially susceptible population.\n",
|
||
|
"\n",
|
||
|
"Let's do some simulations to understand the effect different $R_t$ values would have.\n",
|
||
|
"\n",
|
||
|
"Consider this python code:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"Rt = 1.5\n",
|
||
|
"\n",
|
||
|
"infected = 1\n",
|
||
|
"num_generations = 0\n",
|
||
|
"while infected < 1000:\n",
|
||
|
" infected = infected*Rt\n",
|
||
|
" num_generations += 1\n",
|
||
|
" \n",
|
||
|
"print(f\"It took {num_generations} generations to reach 1000 infected\")\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"If we wanted to write this using mathematical notation, we would write this as\n",
|
||
|
"\n",
|
||
|
"$N_{t+1} = R_t N_t$\n",
|
||
|
"\n",
|
||
|
"where $N_t$ is the number of infected individuals at generation $t$ and $N_{t+1}$ is the number of infected individuals at the next generation ($t+1$).\n",
|
||
|
"\n",
|
||
|
"**What is a \"generation\"?** What we call \"generation\" here is called the serial interval (SI) in the scientific literature. According to [this paper](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6092233/), \"the serial interval (SI), defined as the time between disease symptom onset of a case and that of its infector, is a surrogate for the generation interval— an unobservable quantity defined as the time between the infection of a case and the time of infection of its infector.\" \n",
|
||
|
"\n",
|
||
|
"With regard to SARS-CoV-2 in Germany, according to [the Corona-Warn-App FAQ (Section \"App Features\" / \"Other features\" / \"In-App statistics\")](https://web.archive.org/web/20221105141411/https://www.coronawarn.app/en/faq/results/?search=&topic=application#other_features), \"The concept of the R-value and the data basis of its calculation is described in detail in the Epidemiological Bulletin of the RKI ([#17/2020 of 04/23/2020](https://www.rki.de/DE/Content/Infekt/EpidBull/Archiv/2020/17/Art_02.html)). Sample calculations and the daily updated R-values (4-day and 7-day R-value) are available as an Excel table at http://www.rki.de/covid-19-nowcasting.\" The data from 2 March 2020 to 17 June 2023 are available at [github.com/robert-koch-institut/SARS-CoV-2-Nowcasting_und_-R-Schaetzung](https://github.com/robert-koch-institut/SARS-CoV-2-Nowcasting_und_-R-Schaetzung/blob/main/Nowcast_R_aktuell.csv).\n",
|
||
|
"\n",
|
||
|
"Here is a [guide to R](https://www.nature.com/articles/d41586-020-02009-w) from July 2020.\n",
|
||
|
"\n",
|
||
|
"## Q1\n",
|
||
|
"\n",
|
||
|
"Modify the code above so that you create a variable called `generations_to_1000` which has the number of generations required for 1 person to reach 1000 people infected."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-baf6db377ffe7ab0",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"It took 18 generations to reach 1000 infected\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"Rt = 1.5\n",
|
||
|
"\n",
|
||
|
"infected = 1\n",
|
||
|
"num_generations = 0\n",
|
||
|
"while infected < 1000:\n",
|
||
|
" infected = infected*Rt\n",
|
||
|
" num_generations += 1\n",
|
||
|
"\n",
|
||
|
"print(f\"It took {num_generations} generations to reach 1000 infected\")\n",
|
||
|
"generations_to_1000 = num_generations"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-38f63e894825dd02",
|
||
|
"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(generations_to_1000)=='4ec9599fc2'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-f87155f652e119f8",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q2\n",
|
||
|
"\n",
|
||
|
"Now create a function called `find_cycles_to_reach_1000` which takes 1 argument, `Rt`. This function should return the number of cycles required to reach 1000 infected people.\n",
|
||
|
"\n",
|
||
|
"Your function signature should look like this:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
" def find_generations_to_reach_1000(Rt):\n",
|
||
|
"```"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-c3cb6fb8fbd84f76",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def find_generations_to_reach_1000(Rt):\n",
|
||
|
" infected = 1\n",
|
||
|
" num_generations = 0\n",
|
||
|
" while infected < 1000:\n",
|
||
|
" infected = infected*Rt\n",
|
||
|
" num_generations += 1\n",
|
||
|
"\n",
|
||
|
" return num_generations "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-abfc63314cbd6f15",
|
||
|
"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(find_generations_to_reach_1000(1.1))=='96061e92f5'\n",
|
||
|
"assert ads_hash(find_generations_to_reach_1000(1.5))=='4ec9599fc2'\n",
|
||
|
"assert ads_hash(find_generations_to_reach_1000(2.0))=='4a44dc1536'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-5c2ff80d01aae555",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q3\n",
|
||
|
"\n",
|
||
|
"Now create a function called `simulate_generation` which takes two arguments, `a` and `Rt`. This function should return `None`. The first argument `a` will be a list with the number of infected individuals and `Rt` is the effective reproduction number, as before. The function should compute the number of newly infected individuals after one generation and append this to the list passed as the first argument.\n",
|
||
|
"\n",
|
||
|
"Your function signature should look like this:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
" def simulate_generation(a,Rt):\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"Here is an example that works:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"b = [1]\n",
|
||
|
"simulate_generation(b,3)\n",
|
||
|
"simulate_generation(b,3)\n",
|
||
|
"simulate_generation(b,3)\n",
|
||
|
"b\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"In this above example, `b` would be equal to `[1, 3, 9, 27]` at the end.\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-c8679e225738c336",
|
||
|
"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)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-a97791d9f3b1166a",
|
||
|
"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",
|
||
|
"aa = [1]\n",
|
||
|
"assert ads_hash(simulate_generation(aa, 22)) == 'dc937b5989'\n",
|
||
|
"assert ads_hash(aa)=='c7e4ccf872'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-b81fa7ce231855ae",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q4\n",
|
||
|
"\n",
|
||
|
"Now create a function called `calculate_timeseries_to_1000` which takes one argument, `Rt`. This function should return a list containing the number of infected individuals after each cycle up to and including the first value over 1000 individuals.\n",
|
||
|
"\n",
|
||
|
"Your function signature should look like this:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
" def calculate_timeseries_to_1000(Rt):\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"Your code should work so that `calculate_timeseries_to_1000(2)` would return `[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]` and that `calculate_timeseries_to_1000(3)` would return `[1, 3, 9, 27, 81, 243, 729, 2187]`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-1c6492f08ae6ad56",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def calculate_timeseries_to_1000(Rt):\n",
|
||
|
" a = [1]\n",
|
||
|
" while a[-1] < 1000:\n",
|
||
|
" simulate_generation(a,Rt)\n",
|
||
|
" return a"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-2c8986f98612af75",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"assert ads_hash(calculate_timeseries_to_1000(2))=='a9ad958975'\n",
|
||
|
"assert ads_hash(calculate_timeseries_to_1000(3))=='7aa0d3bbca'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-f196cf19e3293382",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Bonus - plotting the results\n",
|
||
|
"\n",
|
||
|
"Let's now see the result of these simulations."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-de643bc87190397a",
|
||
|
"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": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-24654bdb7a3d2e91",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkQAAAGwCAYAAABIC3rIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABv8ElEQVR4nO3dd3xUVf7/8dekTQrJkATSMDTpJKCC0hRUpK1gwRUVxYaKPxFEYO1tXQV1V2CFRdRFsSHud1dcd1eQogYRUQhEihhaKIGEUNJ7Zu7vj2EGIi0TkkzJ+/l4zCMz956Z+7lEk0/O+ZxzTIZhGIiIiIg0Yn7uDkBERETE3ZQQiYiISKOnhEhEREQaPSVEIiIi0ugpIRIREZFGTwmRiIiINHpKiERERKTRC3B3AN7CZrNx8OBBwsPDMZlM7g5HREREasAwDAoLC0lISMDP78z9QEqIaujgwYMkJia6OwwRERGphf3793PBBRec8bwSohoKDw8H7P+gERERbo5GREREaqKgoIDExETn7/EzUUJUQ45hsoiICCVEIiIiXuZc5S4qqhYREZFGTwmRiIiINHpKiERERKTRUw2RiIjIWVitViorK90dhpxBYGAg/v7+5/05SohEREROwzAMsrOzycvLc3cocg5NmzYlLi7uvNYJVEIk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OLFi3nqqacAe6H1ubalOt8hs3MtrwMwZ84cFi9ezMqVKwGYOnUqI0aMoGXLluTk5PDSSy9RUFDAXXf
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.plot(calculate_timeseries_to_1000(1.1), label=\"$R_t$ = 1.1\")\n",
|
||
|
"plt.plot(calculate_timeseries_to_1000(1.5), label=\"$R_t$ = 1.5\")\n",
|
||
|
"plt.plot(calculate_timeseries_to_1000(2.0), label=\"$R_t$ = 2.0\")\n",
|
||
|
"plt.xlabel(\"cycles\")\n",
|
||
|
"plt.ylabel(\"number of infected individuals\")\n",
|
||
|
"plt.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-a060dcd11e607e7a",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# String formatting\n",
|
||
|
"\n",
|
||
|
"Remember from lecture various ways of formatting strings with template in Python. We are going to use f-strings here. \n",
|
||
|
"\n",
|
||
|
"Here is an example:\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"city=\"Freiburg\"\n",
|
||
|
"continent=\"Europe\"\n",
|
||
|
"result = f\"The city {city} is in the continent {continent}\"\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"This will set `result` to `'The city Freiburg is in the continent Europe'`.\n",
|
||
|
"\n",
|
||
|
"## Q1\n",
|
||
|
"\n",
|
||
|
"Create a function named `greeting` which takes two arguments, `name` and `age`. It should return a string with these values inserted in to a string so that when called like\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"result = greeting(\"Andrew\",7)\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"`result` will be `'Hello. My name is Andrew. My age is 7'`.\n",
|
||
|
"\n",
|
||
|
"The function signature will be\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"def greeting(name, age):\n",
|
||
|
"```"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-3849b2b9c48cf490",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def greeting(name, age):\n",
|
||
|
" return f\"Hello. My name is {name}. My age is {age}\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-5e904f2c9578e11c",
|
||
|
"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(greeting(\"Andrew\",7))=='1370611a0d'\n",
|
||
|
"assert ads_hash(greeting(\"Bob\",47))=='3b8fd03444'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Running Python from the terminal\n",
|
||
|
"\n",
|
||
|
"# Q1\n",
|
||
|
"\n",
|
||
|
"Create a Python script called `my_name.py` which does two things:\n",
|
||
|
"\n",
|
||
|
"1) prints your name\n",
|
||
|
"2) computes the value of 1001 * 22 and then prints this\n",
|
||
|
"\n",
|
||
|
"# Final upload of your assignment today\n",
|
||
|
"\n",
|
||
|
"When you upload your assignnment, you should upload:\n",
|
||
|
"\n",
|
||
|
"1. This `1__Flow_control_Plotting_Strings.ipynb` notebook file as you completed it. Remember not to change its name.\n",
|
||
|
"2. The Python script `my_name.py` which does the correct actions. Both these files should be placed in the `release/exercise-04` directory."
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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
|
||
|
}
|