Week 4: Loops

Warning

This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.

Week 4: Closure#

Syllabus#

  • Looping over a sequence (known number of iterations), the keywords for and in and the range() function

  • Understanding that indexing variable in the for loop is reassigned with each iteration

  • Looping as long as a condition is true (unknown number of iterations), the keyword while

  • Understanding that the variables used in the condition need to be defined before the while loop

  • Understanding that the variables used in the condition should be changed in the while loop body for loop to terminate

  • The keyword break

  • Solve simple problems that require a lot of computation e.g. simulations and population models

  • The concept of pseudocode

Checkpoints#

Checkpoint 4.1: Disease Simulation#

We investigate an SIR (susceptible, infectious, recovered) model for an infectious disease. The model is based on the following assumptions:

  • People can be in one of three states: susceptible \(S_t\), infectious \(I_t\), or recovered \(R_t\). These values change over time \(t\), but the total population size \(S_t + I_t + R_t\) remains constant.

  • The infection rate \(\beta\) determines how many susceptible people an infectious person will infect each day. The number of people becoming infectious each day is \(\beta S_t I_t\).

  • The recovery rate \(\gamma\) determines how many infectious people will recover each day. The number of people recovering each day is \(\gamma I_t\).

The model is:

\[\begin{split} \begin{aligned} \nonumber S_{t+1} &= S_t - \beta S_t I_t \\ \nonumber I_{t+1} &= I_t + \beta S_t I_t - \gamma I_t \\ \nonumber R_{t+1} &= R_t + \gamma I_t \end{aligned} \end{split}\]

Write a script that simulates the spread of a disease over 100 days. You should always start with a population where only one person is infectious, and the rest are susceptible. You shouldn’t round the calculations, but you should print the rounded values.

For example, try the following parameters:

population = 1000
infectious = 1
infection_rate = 0.0004
recovery_rate = 0.2
days = 100

Print the number of susceptible, infectious, and recovered people every tenth day. The first few lines of the output should look like this:

Day 10, Susceptible: 989, Infectious: 6, Recovered: 5
Day 20, Susceptible: 930, Infectious: 34, Recovered: 36
Day 30, Susceptible: 699, Infectious: 125, Recovered: 176

Checkpoint 4.2: Which Tetrahedral#

The \(n^\text{th}\) tetrahedral number is given by

\[ T_n = \frac{n(n+1)(n+2)}{6} \]

and a sequence of tetrahedral numbers is

\[ 1,\, 4,\, 10,\, 20,\, 35,\, 56,\, 84,\, 120,\, 165,\, 220, \ldots \]

For an integer \(t\), you are interested in knowing whether \(t\) is a tetrahedral number, and if so, which number in the sequence it is. Write a script that finds the \(n\) such that \(T_n = t\) for a given \(t\). If \(t\) is not a tetrahedral number, the script should print that \(t\) is not a tetrahedral number.

For example, the script starting with t = 121 should print:

121 is not a tetrahedral number

Given other starting values, your script should print the following:

171700 is a tetrahedral number with n = 100
227920 is a tetrahedral number with n = 110
222439000 is not a tetrahedral number