Lab 8: Midterm Review

Due at 11:59pm on Friday, 03/16/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 and 2 and submit through Ok. Question 1 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.
  • In order to facilitate midterm studying, solutions to this lab will be released on Wednesday, March 14. We encourage you to try out the problems and struggle for a while before looking at the solutions!

Required Questions

Linked Lists

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, which consists of the number of non-Link elements in the linked list and the sum of the deep length of all linked lists elements. 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.

    >>> deep_len(Link(1, Link(2, Link(3))))
    3
    >>> deep_len(Link(Link(1, Link(2)), Link(3, Link(4))))
    4
    >>> levels = Link(Link(Link(1, Link(2)), \
            Link(3)), Link(Link(4), Link(5)))
    >>> print(levels)
    <<<1 2> 3> <4> 5>
    >>> deep_len(levels)
    5
    """
"*** YOUR CODE HERE ***"
if lnk is Link.empty: return 0 elif not isinstance(lnk, Link): return 1 else: return deep_len(lnk.first) + deep_len(lnk.rest)

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.

Optional Questions

Objects

Q3: WWPP: Methods

Use Ok to test your knowledge with the following "What Would Python Print?" questions:

python3 ok -q foobar -u

Hint: Remember for all WWPP questions, enter Function if you believe the answer is <function ...> and Error if it errors.

>>> class Foo:
...     def print_one(self):
...         print('foo')
...     def print_two():
...         print('foofoo')
>>> f = Foo()
>>> f.print_one()
______
foo
>>> f.print_two()
______
Error
>>> Foo.print_two()
______
foofoo
>>> class Bar(Foo): ... def print_one(self): ... print('bar') >>> b = Bar() >>> b.print_one()
______
bar
>>> Bar.print_two()
______
foofoo
>>> Bar.print_one = lambda x: print('new bar') >>> b.print_one()
______
new bar

Q4: WWPP: Attributes

Use Ok to test your knowledge with the following "What Would Python Print?" questions:

python3 ok -q attributes -u

Hint: Remember for all WWPP questions, enter Function if you believe the answer is <function ...> and Error if it errors.

>>> class Foo:
...     a = 10
...     def __init__(self, a):
...         self.a = a
>>> class Bar(Foo):
...     b = 1
>>> a = Foo(5)
>>> b = Bar(2)
>>> a.a
______
5
>>> b.a
______
2
>>> Foo.a
______
10
>>> Bar.b
______
1
>>> Bar.a
______
10
>>> b.b
______
1
>>> Foo.c = 15 >>> Foo.c
______
15
>>> a.c
______
15
>>> b.c
______
15
>>> Bar.c
______
15
>>> b.b = 3 >>> b.b
______
3
>>> Bar.b
______
1

Q5: Keyboard

We'd like to create a Keyboard class that takes in an arbitrary number of Buttons and stores these Buttons 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.typing([0, 1])
    'HI'
    >>> k.typing([1, 0])
    'IH'
    >>> b1.pressed
    2
    >>> b2.pressed
    3
    """

    def __init__(self, *args):
"*** YOUR CODE HERE ***"
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"""
"*** YOUR CODE HERE ***"
if info in self.buttons.keys(): b = self.buttons[info] b.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"""
"*** YOUR CODE HERE ***"
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.pressed = 0

Use Ok to test your code:

python3 ok -q Keyboard

Nonlocal

Q6: Advanced Counter

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:

    >>> make_counter = make_advanced_counter_maker()
    >>> 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
    """
"*** YOUR CODE HERE ***"
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

def avocadis(lst, i, lst0):
    lst0.append(lst.pop(i))
    return len(lst0)

bananis = [1, 6, 1, 6]
n = bananis[3]
we = got(bananis, n, avocadis)

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.

Q8: Trade

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]
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]
    >>> trade(a, b) # Trades 1+1+3+2=7 for 4+3=7
    'Deal!'
    >>> a
    [4, 3, 1, 1, 4]
    >>> b
    [1, 1, 3, 2, 2, 7]
    >>> c = [3, 3, 2, 4, 1]
    >>> trade(b, c)
    'No deal!'
    >>> b
    [1, 1, 3, 2, 2, 7]
    >>> c
    [3, 3, 2, 4, 1]
    >>> trade(a, c)
    'Deal!'
    >>> a
    [3, 3, 2, 1, 4]
    >>> b
    [1, 1, 3, 2, 2, 7]
    >>> c
    [4, 3, 1, 4, 1]
    """
    m, n = 1, 1

"*** YOUR CODE HERE ***"
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

Recursive objects

Q9: 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("(", " . ", ")", "()")
    >>> lst = Link(1, Link(2, Link(3, Link(4))))
    >>> kevins_to_string(lst)
    '[1|-]-->[2|-]-->[3|-]-->[4|-]-->[]'
    >>> kevins_to_string(Link.empty)
    '[]'
    >>> jerrys_to_string(lst)
    '(1 . (2 . (3 . (4 . ()))))'
    >>> jerrys_to_string(Link.empty)
    '()'
    """
"*** YOUR CODE HERE ***"
def printer(lnk): if lnk is Link.empty: return empty_repr else: return front + str(lnk.first) + mid + printer(lnk.rest) + back return printer

Use Ok to test your code:

python3 ok -q make_to_string

Q10: 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
    """
"*** YOUR CODE HERE ***"
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])

Use Ok to test your code:

python3 ok -q tree_map

Q11: 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)
    [Link(0, Link(11, Link(12, Link(13, Link(14)))))]
    """
"*** YOUR CODE HERE ***"
paths = [] if n <= 0 and tree.is_leaf(): paths.append(Link(tree.label)) for b in tree.branches: for path in long_paths(b, n - 1): paths.append(Link(tree.label, path)) return paths

Use Ok to test your code:

python3 ok -q long_paths

More Orders of Growth

Q12: Zap (Orders of Growth)

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 answer:

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 contant work, so the overall runtime will be log n.

Q13: Boom (Orders of Growth)

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 answer:

python3 ok -q boom -u

θ(n4)