Appendix C — Homework: Branching and Looping

Submit this homework via Dropbox. For the deadline please consult the schedule posted on Blackboard.

  • Python-scripts are submitted in one file called: homework2.py

C.1 Exercise 1

Write a program that generates all odd numbers from \(1\) to \(n\). Set n in the beginning of the program and use a while loop to compute the numbers. Make sure that if \(n\) is an even number, the largest generated odd number is \(n-1\). So if \(n=10\) you need to generate numbers \(1,3,...,9\)

I'm fully aware that there are solutions out there that do not use a loop structure. However, the point of this exercise is for you to learn what loops do. So please use the while command.

C.2 Exercise 2

You are given the following program:

a = [1, 3, 5, 7, 11]
b = [13, 17]
c = a + b
print(c)
b[0] = -1
d = [e+1 for e in a]
print(d)
d.append(b[0] + 1)
d.append(b[-1] + 1)
print(d[-2:])

Explain what each line of code does! Just add it as a comment/note with the # symbol in between the code lines.

C.3 Exercise 3

The following code is supposed to compute the sum of fractions as: \(s = \sum_{k=1}^{M}\frac{1}{k}\)

where the formula explicitly means the following: \(s = \sum_{k=1}^{M}\frac{1}{k} = \frac{1}{1} + \frac{1}{2} + ... +\frac{1}{M}\)

Here is the code:

s = 0; k = 1; M = 100
while k < M:
    s += 1/k
print s

This program does not work correctly. What are the errors? (If you try to run the program, nothing will happen on the screen. Type Ctrl-C, i.e., hold down the Control Ctrl key and then type the c key, to stop a program. Fix the program so it runs through and calculates the sum correctly. There are two basic ways to find errors in a program:

  1. read the program carefully and think about the consequences of each statement, and
  2. print out intermediate results and compare with calculations you did by hand.

First, try method (1) and find as many errors as you can. Then, try method (2) for \(M = 3\) and compare the evolution of \(s\) with your calculations by hand.

C.4 Exercise 4

Rewrite the corrected version of the program in Exercise 3 using a for loop over \(k\) values instead of a while loop.

C.5 Exercise 5

We define the following nested list: q = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]

Index this list to extract:
  1. the letter a;
  2. the list ['d', 'e', 'f'];
  3. the last element h;
  4. the d element.
  5. Explain why q[-1][-2] has the value g.

NOTE: You can skip this for the R-component of the course. Nested lists are tricky to do in R.

C.6 Exercise 6

Building a list. Start with the following list of students:

ll = ['john', 'james', 'jim', 'jason', 'jill', 'jane', 'jinn']

Here are their student numbers:

stud_numbers = [345, 123, 876, 234, 198, 456, 876]

Write a program with a loop (for or while that's up to you) that generates a new list so that you have each student name, followed by her/his student number, i.e.:

database = ['john', 345, 'james', 124, ... , 'jinn', 876]

Once you have generated the new database list write another loop that prints the following output:

------------------------------
Student Database
------------------------------
Name:            ID number:
------------------------------
john             345
james            124
...
jinn             876
------------------------------

Try to make it look as close as possible to this table. This may require the .format() function.