Solutions: You can find the file with solutions for all questions here.

Quiz submissions were graded automatically for correctness. Implementations did not need to be efficient, as long as they were correct.

In addition to the doctests provided to students, we also used extra doctests to check for corner cases. These extra test cases are highlighted below.

Reminder. Every node in a tree t is the root value of some subtree of t.

Definition. A subset S of nodes is tree-consistent if, for every node n included in S, S also includes all nodes in the subtree for which n is the root value. In other words, a tree-consistent subset of nodes contains all the nodes in a collection of subtrees.

For any tree, the set of all nodes and the empty set are both tree-consistent.


x = tree(-1, [tree(1),
              tree(3, [tree(-4),
y = tree(1, [tree(-1, [tree(2)]),
             tree( 8, [tree(-7)]),
             tree(-3, [tree(-3)]),
             tree(-4, [tree(3),
                       tree(7, [tree(-4),

In x, if 3 is included then -4 and -5 must be included because 3 is the root value of a subtree containing -4 and -5. Therefore, the set of nodes {3, -4, -5} is tree-consistent, but {3} and {3, -4} are not. The tree-consistent subset with the highest sum is {1}.

In y, the tree-consistent subset with the highest sum is {2, 8, -7, 3, 7, -4, -2}:

Question 1: Subtree Sum

Implement subtreesum, which takes a tree t as an argument. It returns the maximum sum of a tree-consistent subset of the nodes in t.

def subtreesum(t):
    """Return the maximum sum of any tree-consistent subset of nodes in t.

    >>> subtreesum(tree(5))
    >>> subtreesum(tree(-5, [tree(6)]))
    >>> subtreesum(tree(-5, [tree(6, [tree(-2)])]))
    >>> subtreesum(tree(-2))

    >>> subtreesum(x)
    >>> subtreesum(y)
    >>> subtreesum(tree(20, branches(y))) # Max sum includes all nodes
    return max(treesum(t), sum([subtreesum(b) for b in branches(t)]))

def treesum(t):
    """Return the sum of all node values in t."""
    return root(t) + sum([treesum(b) for b in branches(t)])

Use OK to test your code:

python3 ok -q subtreesum

Question 2: Mint

Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth.

  • Each Mint instance has a year stamp. The update method sets the year stamp to the current_year class attribute of the Mint class.
  • The create method takes a subclass of Coin and returns an instance of that class stamped with the mint's year (which may be different from Mint.current_year if it has not been updated.)
  • A Coin's worth method returns the cents value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from the current_year class attribute of the Mint class.

Use OK to test your code:

python3 ok -q Mint
class Mint:
    """A mint creates coins by stamping on years.

    The update method sets the mint's stamp to Mint.current_year.

    >>> mint = Mint()
    >>> mint.year
    >>> dime = mint.create(Dime)
    >>> dime.year
    >>> Mint.current_year = 2100  # Time passes
    >>> nickel = mint.create(Nickel)
    >>> nickel.year     # The mint has not updated its stamp yet
    >>> nickel.worth()  # 5 cents + (85 - 50 years)
    >>> mint.update()   # The mint's year is updated to 2100
    >>> Mint.current_year = 2175     # More time passes
    >>> mint.create(Dime).worth()    # 10 cents + (75 - 50 years)
    >>> Mint().create(Dime).worth()  # A new mint has the current year
    >>> dime.worth()     # 10 cents + (160 - 50 years)
    >>> Dime.cents = 20  # Upgrade all dimes!
    >>> dime.worth()     # 20 cents + (160 - 50 years)
    current_year = 2015

    def __init__(self):

    def create(self, kind):
        return kind(self.year)

    def update(self):
        self.year = Mint.current_year

class Coin:
    def __init__(self, year):
        self.year = year

    def worth(self):
        "The worth is a coin's face value + 1 cent for each year over age 50."
        return self.cents + max(0, Mint.current_year - self.year - 50)

class Nickel(Coin):
    cents = 5

class Dime(Coin):
    cents = 10