Note: If you wish to meet with me one on one to discuss the course, please shoot me an email with your available times. My email can be found on the course info page.

Question 1

Draw the environment diagram.

def reverse(lst):
    if len(lst) <= 1:
        return lst
    return reverse(lst[1:]) + [lst[0]]

l = [1, [2, 3], 4]
rev = reverse(l)

Followup: How would you modify this to deep-reverse a list - that is, if an element of the list is also a list, that element gets reversed as well (and all lists under it)?


Question 2

What does the following code print?

lst = [None for _ in range(10)]
for i in range(10):
    lst[i] = lambda: i

for func in lst:
    print(func())

Question 3

Implement a function addup that takes in a lst of numbers and a target value n. It should return True if some subset of lst adds up to n. Numbers in lst may not be reused. For example:

>>> addup([3, 4, 5], 9)
True # 4 + 5 = 9
>>> addup([3, 9, 27, 81], 3)
True # 3 = 3
>>> addup([3, 9, 27, 81], 33)
False

Followup: What needs to be changed if values in lst can be reused?