CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Quiz 3: Questions

Q3.1.

[6 pts] Convert the following to an if statement where not does not appear.

if not (a < b or a < c):
Q3.2.

[8 pts] Suppose census is a dictionary mapping state names to populations. Complete the following so that it displays the total population of all the states.



for state in census:



print total_pop
Q3.3.

[8 pts] Draw a recursion tree representing the recursive calls made when computing the value of f(24) using the following function.

def f(n):
    k = 1
    for i in range(2, n - 1):
        if n % i == 0:
            k += f(i)
    return k
Q3.4.

[8 pts] Without using loops (i.e., by using recursion), and also without using other functions or methods except len, create a function remove_rs that takes a string parameter returns that string with all lower-case r's removed. For example, remove_rs('proper') should return pope, and remove_rs('corral') should return coal.

Quiz 3: Solutions

Q3.1.
if a >= b and a >= c:
Q3.2.
total_pop = 0
for state in census:
    total_pop += census[state]
print total_pop
Q3.3.
Q3.4.
def remove_rs(word):
    if len(word) == 0:
        return ''
    elif word[-1] == 'r':
        return remove_rs(word[:-1])
    else:
        return remove_rs(word[:-1]) + word[-1]