# Lab 8: Midterm Review lab08.zip

Due at 11:59pm on Friday, 10/19/2018.

## Starter Files

Download lab08.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.

## Submission

By the end of this lab, you should have submitted the lab with `python3 ok --submit`. You may submit more than once before the deadline; only the final submission will be graded. Check that you have successfully submitted your code on okpy.org.

• To receive credit for this lab, you must complete Questions 1 through 4 and submit through Ok. Questions 1, 3, and 4 can be found in lab08.py.
• The remaining questions are extra practice. They can be found in the lab08_extra.py file. It is recommended that you complete these problems after finishing the required portion. There are many optional questions on this lab: it is recommended you identify the topics you struggle with most and complete those questions first.
• In order to facilitate midterm studying, solutions to this lab will be released on Monday, October 15th. We encourage you to try out the problems and struggle for a while before looking at the solutions!

# Required Questions

### Q1: Deep Linked List Length

A linked list that contains one or more linked lists as elements is called a deep linked list. Write a function `deep_len` that takes in a (possibly deep) linked list and returns the deep length of that linked list. The deep length of a linked list is the total number of non-link elements in the list, as well as the total number of elements contained in all contained lists. See the function's doctests for examples of the deep length of linked lists.

Hint: Use `isinstance` to check if something is an instance of an object.

``````def deep_len(lnk):
""" Returns the deep length of a possibly deep linked list.

3
4
>>> print(levels)
<<<1 2> 3> <4> 5>
>>> deep_len(levels)
5
"""
return 0
return 1
else:
return deep_len(lnk.first) + deep_len(lnk.rest)

Video walkthrough: https://youtu.be/pbMeCRUU7yw?t=2m28s``````

Use Ok to test your code:

``python3 ok -q deep_len``

## Orders of Growth

### Q2: Finding Orders of Growth

Use Ok to test your knowledge with the following questions:

``python3 ok -q growth -u``

Be sure to ask a lab assistant or TA if you don't understand the correct answer!

What is the order of growth of `is_prime` in terms of `n`?

``````def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True``````
θ(n).

Explanation: The body of the for loop is executed n - 2 times. Each iteration takes constant time (one conditional check and one return statement). Therefore, the total time is (n - 2) x θ(1), or simply θ(n).

What is the order of growth of `bar` in terms of `n`?

``````def bar(n):
i, sum = 1, 0
while i <= n:
sum += biz(n)
i += 1
return sum

def biz(n):
i, sum = 1, 0
while i <= n:
sum += i**3
i += 1
return sum``````
θ(n2).

Explanation: The body of the while loop in `bar` is executed n times. Each iteration, one call to `biz(n)` is made. Note that n never changes, so this call takes the same time to run each iteration. Taking a look at `biz`, we see that there is another while loop. Be careful to note that although the term being added to `sum` is cubed (`i**3`), `i` itself is only incremented by 1 in each iteration. This tells us that this while loop also executes n times, with each iteration taking constant time , so the total time of `biz(n)` is n x θ(1), or θ(n). Knowing the runtime of `biz(n)`, we can conclude that each iteration of the while loop in `bar` takes θ(n). Therefore, the total runtime of `bar(n)` is n x θ(n).

What is the order of growth of `foo` in terms of `n`, where `n` is the length of `lst`? Assume that slicing a list and calling `len` on a list can both be done in constant time.

``````def foo(lst, i):
mid = len(lst) // 2
if mid == 0:
return lst
elif i > 0:
return foo(lst[mid:], -1)
else:
return foo(lst[:mid], 1)``````
θ(log(n)).

Explanation: A single recursive call is made in the body of `foo` on half the input list (either the first half or the second half depending on the input flag `i`). The base case is executed when the list either is empty or has only one element. We start with an n element list and halve the list until there is at most 1 element, which means there will be log(n) total calls. Each call, constant work is done if we ignore the recursive call. The total runtime is then log(n) * θ(1).

Note: We simplified this problem by assuming that slicing a list takes constant time. In reality, this operation is a bit more nuanced and may take linear time. As an additional exercise, try determining the order of growth of this function if we assuming slicing takes linear time.

## Recursion & Tree Recursion

### Q3: Subsequences

A subsequence of a sequence `S` is a sequence of elements from `S`, in the same order they appear in `S`, but possibly with elements missing. Thus, the lists `[]`, `[1, 3]`, `[2]`, and `[1, 2, 3]` are some (but not all) of the subsequences of `[1, 2, 3]`. Write a function that takes a list and returns a list of lists, for which each individual list is a subsequence of the original input.

In order to accomplish this, you might first want to write a function `insert_into_all` that takes an item and a list of lists, adds the item to the beginning of nested list, and returns the resulting list.

``````def insert_into_all(item, nested_list):
"""Assuming that nested_list is a list of lists, return a new list
consisting of all the lists in nested_list, but with item added to
the front of each.

>>> nl = [[], [1, 2], [3]]
>>> insert_into_all(0, nl)
[[0], [0, 1, 2], [0, 3]]
"""
return [[item] + lst for lst in nested_list]
def subseqs(s):
"""Assuming that S is a list, return a nested list of all subsequences
of S (a list of lists). The subsequences can appear in any order.

>>> seqs = subseqs([1, 2, 3])
>>> sorted(seqs)
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
>>> subseqs([])
[[]]
"""
if not s:
return [[]]
else:
subset = subseqs(s[1:])
return insert_into_all(s[0], subset) + subset``````

Use Ok to test your code:

``python3 ok -q subseqs``

### Q4: Increasing Subsequences

Now, we wish to find subsequences subject to another condition: we only want the subsequences for which consecutive elements are nondecreasing. For example, `[1, 3, 2]` is a subsequence of `[1, 3, 2, 4]`, but since 2 < 3, this subsequence would not be included in our result.

Fill in the blanks to complete the implementation of the `inc_subseqs` function. You may assume that the input list only contains positive elements.

You may use the helper function `insert_into_all` you defined in the previous part.

``````def inc_subseqs(s):
"""Assuming that S is a list, return a nested list of all subsequences
of S (a list of lists) for which the elements of the subsequence
are strictly nondecreasing. The subsequences can appear in any order.

>>> seqs = inc_subseqs([1, 3, 2])
>>> sorted(seqs)
[[], [1], [1, 2], [1, 3], [2], [3]]
>>> inc_subseqs([])
[[]]
>>> seqs2 = inc_subseqs([1, 1, 2])
>>> sorted(seqs2)
[[], [1], [1], [1, 1], [1, 1, 2], [1, 2], [1, 2], [2]]
"""
def subseq_helper(s, prev):
if not s:
return ____________________
return [[]]        elif s[0] < prev:
return ____________________
return subseq_helper(s[1:], prev)        else:
a = ______________________
a = subseq_helper(s[1:], s[0])            b = ______________________
b = subseq_helper(s[1:], prev)            return insert_into_all(________, ______________) + ________________
return insert_into_all(s[0], a) + b    return subseq_helper(____, ____)
return subseq_helper(s, 0)``````

Use Ok to test your code:

``python3 ok -q inc_subseqs``

# Optional Questions

## Objects

### Q5: Keyboard

We'd like to create a `Keyboard` class that takes in an arbitrary number of `Button`s and stores these `Button`s in a dictionary. The keys in the dictionary will be ints that represent the postition on the `Keyboard`, and the values will be the respective `Button`. Fill out the methods in the `Keyboard` class according to each description, using the doctests as a reference for the behavior of a `Keyboard`.

``````class Keyboard:
"""A Keyboard takes in an arbitrary amount of buttons, and has a
dictionary of positions as keys, and values as Buttons.

>>> b1 = Button(0, "H")
>>> b2 = Button(1, "I")
>>> k = Keyboard(b1, b2)
>>> k.buttons[0].key
'H'
>>> k.press(1)
'I'
>>> k.press(2) #No button at this position
''
>>> k.typing([0, 1])
'HI'
>>> k.typing([1, 0])
'IH'
>>> b1.times_pressed
2
>>> b2.times_pressed
3
"""

def __init__(self, *args):
self.buttons = {}
for button in args:
self.buttons[button.pos] = button
def press(self, info):
"""Takes in a position of the button pressed, and
returns that button's output"""
if info in self.buttons.keys():
b = self.buttons[info]
b.times_pressed += 1
return b.key
return ''
def typing(self, typing_input):
"""Takes in a list of positions of buttons pressed, and
returns the total output"""
accumulate = ''
for pos in typing_input:
accumulate+=self.press(pos)
return accumulate
class Button:
def __init__(self, pos, key):
self.pos = pos
self.key = key
self.times_pressed = 0``````

Use Ok to test your code:

``python3 ok -q Keyboard``

## Nonlocal

Complete the definition of `make_advanced_counter_maker`, which creates a function that creates counters. These counters can not only update their personal count, but also a shared count for all counters. They can also reset either count.

``````def make_advanced_counter_maker():
"""Makes a function that makes counters that understands the
messages "count", "global-count", "reset", and "global-reset".
See the examples below:

>>> tom_counter = make_counter()
>>> tom_counter('count')
1
>>> tom_counter('count')
2
>>> tom_counter('global-count')
1
>>> jon_counter = make_counter()
>>> jon_counter('global-count')
2
>>> jon_counter('count')
1
>>> jon_counter('reset')
>>> jon_counter('count')
1
>>> tom_counter('count')
3
>>> jon_counter('global-count')
3
>>> jon_counter('global-reset')
>>> tom_counter('global-count')
1
"""
global_count = 0
def make_counter():
count = 0
def counter(msg):
nonlocal global_count, count
if msg == 'count':
count += 1
return count
elif msg == 'reset':
count = 0
elif msg == 'global-count':
global_count += 1
return global_count
elif msg == 'global-reset':
global_count = 0
return counter
return make_counter``````

Use Ok to test your code:

``python3 ok -q make_advanced_counter_maker``

## Mutable Lists

### Q7: Environment Diagram

Draw an environment diagram for the following program.

Some things to remember:

• When you mutate a list, you are changing the original list.
• When you concatenate two lists, you are creating a new list.
• When you assign a name to an existing object, you are creating another reference to that object rather than creating a copy of that object.
``````def got(lst, el, f):
welcome = []
for e in lst:
if e == el:
el = f(lst[1:], 2, welcome)
return lst[3:] + welcome

lst0.append(lst.pop(i))
return len(lst0)

bananis = [1, 6, 1, 6]
n = bananis[3]

You can check your solution here. If you get stuck, ask a Lab Assistant or TA for help before checking the solution! There is nothing to submit for this problem.

In the integer market, each participant has a list of positive integers to trade. When two participants meet, they trade the smallest non-empty prefix of their list of integers. A prefix is a slice that starts at index 0.

Write a function `trade` that exchanges the first `m` elements of list `first` with the first `n` elements of list `second`, such that the sums of those elements are equal, and the sum is as small as possible. If no such prefix exists, return the string `'No deal!'` and do not change either list. Otherwise change both lists and return `'Deal!'`. A partial implementation is provided.

Hint: You can mutate a slice of a list using slice assignment. To do so, specify a slice of the list `[i:j]` on the left-hand side of an assignment statement and another list on the right-hand side of the assignment statement. The operation will replace the entire given slice of the list from `i` inclusive to `j` exclusive with the elements from the given list. The slice and the given list need not be the same length.

``````>>> a = [1, 2, 3, 4, 5, 6]
>>> b = a
>>> a[2:5] = [10, 11, 12, 13]
>>> a
[1, 2, 10, 11, 12, 13, 6]
>>> b
[1, 2, 10, 11, 12, 13, 6]``````

Additionally, recall that the starting and ending indices for a slice can be left out and Python will use a default value. `lst[i:]` is the same as `lst[i:len(lst)]`, and `lst[:j]` is the same as `lst[0:j]`.

``````def trade(first, second):
"""Exchange the smallest prefixes of first and second that have equal sum.

>>> a = [1, 1, 3, 2, 1, 1, 4]
>>> b = [4, 3, 2, 7]
'Deal!'
>>> a
[4, 3, 1, 1, 4]
>>> b
[1, 1, 3, 2, 2, 7]
>>> c = [3, 3, 2, 4, 1]
'No deal!'
>>> b
[1, 1, 3, 2, 2, 7]
>>> c
[3, 3, 2, 4, 1]
'Deal!'
>>> a
[3, 3, 2, 1, 4]
>>> b
[1, 1, 3, 2, 2, 7]
>>> c
[4, 3, 1, 4, 1]
"""
m, n = 1, 1

equal_prefix = lambda: sum(first[:m]) == sum(second[:n])
while m < len(first) and n < len(second) and not equal_prefix():
if sum(first[:m]) < sum(second[:n]):
m += 1
else:
n += 1
if False: # change this line!
if equal_prefix():        first[:m], second[:n] = second[:n], first[:m]
return 'Deal!'
else:
return 'No deal!'``````

Use Ok to test your code:

``python3 ok -q trade``

## Iterators and Generators

### Q9: Generate Permutations

Given a sequence of unique elements, a permutation of the sequence is a list containing the elements of the sequence in some arbitrary order. For example, `[2, 1, 3]`, `[1, 3, 2]`, and `[3, 2, 1]` are some of the permutations of the sequence `[1, 2, 3]`.

Implement `permutations`, a generator function that takes in a sequence `seq` and returns a generator that yields all permutations of `seq`.

Permutations may be yielded in any order. Note that the doctests test whether you are yielding all possible permutations, but not in any particular order. The built-in `sorted` function takes in an iterable object and returns a list containing the elements of the iterable in non-decreasing order.

Your solution must fit on the lines provided in the skeleton code.

Hint: If you had the permutations of all the elements in `lst` not including the first element, how could you use that to generate the permutations of the full `lst`?

``````def permutations(seq):
"""Generates all permutations of the given sequence. Each permutation is a
list of the elements in SEQ in a different order. The permutations may be
yielded in any order.

>>> perms = permutations([100])
>>> type(perms)
<class 'generator'>
>>> next(perms)
[100]
>>> try:
...     next(perms)
... except StopIteration:
...     print('No more permutations!')
No more permutations!
>>> sorted(permutations([1, 2, 3])) # Returns a sorted list containing elements of the generator
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
>>> sorted(permutations((10, 20, 30)))
[[10, 20, 30], [10, 30, 20], [20, 10, 30], [20, 30, 10], [30, 10, 20], [30, 20, 10]]
>>> sorted(permutations("ab"))
[['a', 'b'], ['b', 'a']]
"""
if ____________________:
if not seq:        yield ____________________
yield []    else:
for perm in _____________________:
for perm in permutations(seq[1:]):            for _____ in ________________:
for i in range(len(seq)):                _________________________
yield perm[:i] + [seq[0]] + perm[i:]``````

Use Ok to test your code:

``python3 ok -q permutations``

## Recursive objects

### Q10: Linked Lists as Strings

Kevin and Jerry like different ways of displaying the linked list structure in Python. While Kevin likes box and pointer diagrams, Jerry prefers a more futuristic way. Write a function `make_to_string` that returns a function that converts the linked list to a string in their preferred style.

Hint: You can convert numbers to strings using the `str` function, and you can combine strings together using `+`.

``````>>> str(4)
'4'
>>> 'cs ' + str(61) + 'a'
'cs 61a'``````
``````def make_to_string(front, mid, back, empty_repr):
""" Returns a function that turns linked lists to strings.

>>> kevins_to_string = make_to_string("[", "|-]-->", "", "[]")
>>> jerrys_to_string = make_to_string("(", " . ", ")", "()")
>>> kevins_to_string(lst)
'[1|-]-->[2|-]-->[3|-]-->[4|-]-->[]'
'[]'
>>> jerrys_to_string(lst)
'(1 . (2 . (3 . (4 . ()))))'
'()'
"""
def printer(lnk):
return empty_repr
else:
return front + str(lnk.first) + mid + printer(lnk.rest) + back
return printer

Video walkthrough: https://youtu.be/pbMeCRUU7yw?t=14m22s``````

Use Ok to test your code:

``python3 ok -q make_to_string``

### Q11: Tree Map

Define the function `tree_map`, which takes in a tree and a one-argument function as arguments and returns a new tree which is the result of mapping the function over the entries of the input tree.

``````def tree_map(fn, t):
"""Maps the function fn over the entries of t and returns the
result in a new tree.

>>> numbers = Tree(1,
...                [Tree(2,
...                      [Tree(3),
...                       Tree(4)]),
...                 Tree(5,
...                      [Tree(6,
...                            [Tree(7)]),
...                       Tree(8)])])
>>> print(tree_map(lambda x: 2**x, numbers))
2
4
8
16
32
64
128
256
>>> print(numbers)
1
2
3
4
5
6
7
8
"""
if t.is_leaf():
return Tree(fn(t.label), [])
mapped_subtrees = [tree_map(fn, b) for b in t.branches]
return Tree(fn(t.label), mapped_subtrees)

# Alternate solution
def tree_map(fn, t):
return Tree(fn(t.label), [tree_map(fn, b) for b in t.branches])

Video walkthrough: https://youtu.be/pbMeCRUU7yw?t=26m47s``````

Use Ok to test your code:

``python3 ok -q tree_map``

### Q12: Long Paths

Implement `long_paths`, which returns a list of all paths in a tree with length at least `n`. A path in a tree is a linked list of node values that starts with the root and ends at a leaf. Each subsequent element must be from a child of the previous value's node. The length of a path is the number of edges in the path (i.e. one less than the number of nodes in the path). Paths are listed in order from left to right. See the doctests for some examples.

``````def long_paths(tree, n):
"""Return a list of all paths in tree with length at least n.

>>> t = Tree(3, [Tree(4), Tree(4), Tree(5)])
>>> left = Tree(1, [Tree(2), t])
>>> mid = Tree(6, [Tree(7, [Tree(8)]), Tree(9)])
>>> right = Tree(11, [Tree(12, [Tree(13, [Tree(14)])])])
>>> whole = Tree(0, [left, Tree(13), mid, right])
>>> for path in long_paths(whole, 2):
...     print(path)
...
<0 1 2>
<0 1 3 4>
<0 1 3 4>
<0 1 3 5>
<0 6 7 8>
<0 6 9>
<0 11 12 13 14>
>>> for path in long_paths(whole, 3):
...     print(path)
...
<0 1 3 4>
<0 1 3 4>
<0 1 3 5>
<0 6 7 8>
<0 11 12 13 14>
>>> long_paths(whole, 4)
"""
paths = []
if n <= 0 and tree.is_leaf():
for b in tree.branches:
for path in long_paths(b, n - 1):
return paths``````

Use Ok to test your code:

``python3 ok -q long_paths``

## More Orders of Growth

### Q13: Boom

What is the order of growth in time for the following function boom? Use big-θ notation.

``````def boom(n):
sum = 0
a, b = 1, 1
while a <= n*n:
while b <= n*n:
sum += (a*b)
b += 1
b = 0
a += 1
return sum``````

Use ok to test your understanding:

``python3 ok -q boom -u``

θ(n4)

### Q14: Zap

What is the order of growth in time for the following function `zap`? Use big-θ notation.

``````def zap(n):
i, count = 1, 0
while i <= n:
while i <= 5 * n:
count += i
print(i / 6)
i *= 3
return count``````

Use ok to test your understanding:

``python3 ok -q zap -u``

θ(log n)

Here, the stopping condition of both loops rely on the same variable `i`. You might notice that completion of the inner loop will guarantee completion of the outer loop; after all, if `i` is greater than `5 * n`, then it will be greater than `n`. Therefore, the overall runtime is just the runtime of the inner loop. Since `i` begins at 1 and is multiplied by 3 at every iteration of the inner loop, the inner loop will have `log n` iterations overall. Each iteration does constant work, so the overall runtime will be `log n`.