## CS21 Lab 3: Numbers, Strings, and For Loops

Due Saturday, February 12, by 11:59pm

### Programming Tips

As you write programs, use good programming practices:

• Use a comment at the top of the file to describe the purpose of the program (see example).

• All programs should have a `main()` function (see example).

• Use variable names that describe the contents of the variables.

• Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

• Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

• Avoid writing any lines of code that exceed 80 columns.

• Always work in a terminal window that is 80 characters wide (resize it to be this wide)

• In `vim`, at the bottom left in the window, there is an indication of both the line and the column of the cursor.

### Are your files in the correct place?

Make sure all programs are saved to your `cs21/labs/03` directory! Files outside that directory will not be graded.

```$update21$ cd ~/cs21/labs/03
$pwd /home/username/cs21/labs/03$ ls
Questions-03.txt
(should see your program files here)```

### Goals

The goals for this lab assignment are:

• Manipulate Python numeric and string data types (`int`, `float`, `str`)

• Learn to iterate over data using `for` loops

• Practice string operations: `+`, `*`

• Practice numeric operations: `+`, `*`

• Practice using the `range` function

• Practice using the accumulator pattern

## 1. Squares

Write a program called `square.py` that asks the user for the side length of a square. The program should then (i) print out the perimeter of the square, (ii) print out the area of the square, and (iii) use `=` symbols to print out a visual representation of the square using text.

Some examples of running the program are shown below. User input is shown in bold.

```$python3 square.py What's the side length? 3 Perimeter is: 12 Area is: 9 = = = = = = = = =$ python3 square.py
What's the side length? 5
Perimeter is: 20
Area is: 25
= = = = =
= = = = =
= = = = =
= = = = =
= = = = =```

Your program should have the following features:

• You can assume the user enters a positive integer for the side length

• Print out the perimeter of the square (the perimeter of a square is the sum of the side lengths)

• Print out the area of the square (the area of a square is the side length squared)

• Use `"="` to print out a text square (remember you can multiply a string by an integer)

• Because characters in the terminal are taller than they are wide, the visual representation will not look square if you print the `=` characters next to each other; instead, try putting a space after each `=` to get a more "square-looking" layout for your square

### 2.1. Background

When epidemiologists model the spread of a disease, one of the simplest models uses a term called $R$-value (or reproduction number) to capture how many people one sick individual will infect (on average). For example, with an $R$-value of 2, if you start with one sick individual, they will infect 2 people; then, each of those will infect another 2, for a total of 4; then each of those 4 will infect 2, for a total of 8, who will infect 16, and so forth. In other words, the function is one of exponential growth; after $c$ cycles of infection, you’ll be infecting $R^c$ people (assuming you started with just one).

There are two different types of $R$-value that are commonly used:

• $R_0$ (R naught) is the baseline reproduction number, and measures how many people (on average) get infected by each person who is sick; it’s essentially a measure of how contagious the disease is. It assumes everyone else is healthy and that nobody is doing anything special or unusual to try to avoid getting sick.

• $R_e$ is the effective reproduction number, which tries to measure how many people (on average) are actually likely to get infected given current real-world conditions. This number is changed by people becoming immune, either after getting sick and recovering, or after getting vaccinated. It’s also impacted by behavior patterns like masking and social distancing.

For the original strain of COVID-19, $R_0$ was estimated to be around 2-3; for the Delta variant it was around 5-6; and for the omicron variant current estimates are around 7-10.

The key to controlling the spread of a pandemic is finding ways to make $R_e$ be small; if $R_e$ is less than 1, the disease will tend to die out over time. We can use this formula to model what might happen if measures like different types of masks or vaccines impact $R_e$ in particular ways.

Note that these are both $R$-values, and mathematically get treated the same way; the only difference is whether you’re using a number that describes the baseline contagiousness, or the effective contagiousness based on immunity and public health measures.

Write a program `spread.py` that models viral spread by asking a user for an $R$-value and a number of cycles, and then printing out how many people will be infected after each cycle.

```$python3 spread.py What's the R value? 2 How many cycles? 3 1.0 2.0 4.0 8.0$ python3 spread.py
What's the R value? 2.6
How many cycles? 4
1.0
2.6
6.76
17.58
45.7

$python3 spread.py What's the R value? 0.7 How many cycles? 5 1.0 0.7 0.49 0.34 0.24 0.17``` Your program should have the following features: • The user selects the R value. You can assume the user enters a positive floating point number. • The user selects the number of cycles. You can assume the user enters a positive integer. • Each line of output should consist of a single floating point number, rounded to 2 decimal places (you can use the `round()` function, which you give the number you want to round and the number of digits after the decimal place you want to keep. For example, `round(1.3333333, 2)` will produce the value `1.33`) • The first line should contain `1.0`, to indicate a single sick individual • Successive lines should contain the number from the previous line multiplied by $R$ (i.e. the number of infections in that iteration) • HINT: this is an example of an accumulator pattern loop • The total number of lines printed should be one more than the number of cycles specified (since the initial `1.0` doesn’t count toward the cycles the user asked for) ### 2.3. Visualizing spread Write another program called `visual.py` that uses dots (`"."`) to show the spread instead of numbers. You can start with a copy of your previous solution, but you’ll need to modify it to change the output style. ```$ python3 visual.py
What's the R value? 2
How many cycles? 3
.

..

....

........

$python3 visual.py What's the R value? 5 How many cycles? 3 . ..... ......................... .............................................................................................................................$ python3 visual.py
What's the R value? 2.6
How many cycles? 4
.

..

......

.................

.............................................

$python3 visual.py What's the R value? 10 How many cycles? 3 . .......... .................................................................................................... ........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................``` Your program should have the following features: • In order to copy your previous program file, you can use the command: `$ cp spread.py visual.py` (but keep in mind that this will replace whatever is in `visual.py` with the contents of `spread.py`, so you only want to do this once when you’ve finished `spread.py` but haven’t started `visual.py` yet)

• The user selects values for R and C as before

• Each line of output should consist of a series of dots (`"."`)

• The number of dots on a line should be calculated like the total number of infections from before, only you’ll need to convert it to an integer (since you can’t print out a fraction of a dot)

• There should be an empty line between each line of dots (so you can tell when the dots go past the end of one line and wrap around); a call to `print()` with nothing between the parens will result in printing a blank line

Each lab will have a short questionnaire at the end. Please edit the `Questions-03.txt` file in your `cs21/labs/03` directory and answer the questions in that file.

Once you’re done with that, you should run `handin21` again.

### Submitting lab assignments

Remember to run `handin21` to turn in your lab files! You may run `handin21` as many times as you want. Each time it will turn in any new work. We recommend running `handin21` after you complete each program or after you complete significant work on any one program.

### Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

#### When Remotely logged in

When you are ssh’ed into the CS labs, first quit any applications you are running, like `vim`, then simply type `exit` at the prompt in your terminal window to disconnect.

#### When Physically logged in

When you are in a CS lab logged into a CS machine. First quit any applications you are running, like the browser and the terminal. Then click on the logout icon ( or ) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!