printable version
Quiz 3
[1]
[2]
[3]
[4]
Problem Q3.1.
[6 pts]
Convert the following to an if statement where not does not appear.
if not (a < b or a < c):
if a >= b and a >= c:
Problem 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
total_pop = 0
for state in census:
total_pop += census[state]
print total_pop
Problem 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
Problem 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.
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]