Quiz 2 Solutions
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.
Examples:
x = tree(-1, [tree(1),
tree(-2),
tree(3, [tree(-4),
tree(-5)])])
y = tree(1, [tree(-1, [tree(2)]),
tree( 8, [tree(-7)]),
tree(-3, [tree(-3)]),
tree(-4, [tree(3),
tree(-5),
tree(7, [tree(-4),
tree(-2)])])])
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))
5
>>> subtreesum(tree(-5, [tree(6)]))
6
>>> subtreesum(tree(-5, [tree(6, [tree(-2)])]))
4
>>> subtreesum(tree(-2))
0
>>> subtreesum(x)
1
>>> subtreesum(y)
7
>>> subtreesum(tree(20, branches(y))) # Max sum includes all nodes
11
"""
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 ayear
stamp. Theupdate
method sets theyear
stamp to thecurrent_year
class attribute of theMint
class. - The
create
method takes a subclass ofCoin
and returns an instance of that class stamped with themint
's year (which may be different fromMint.current_year
if it has not been updated.) - A
Coin
'sworth
method returns thecents
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 thecurrent_year
class attribute of theMint
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
2015
>>> dime = mint.create(Dime)
>>> dime.year
2015
>>> Mint.current_year = 2100 # Time passes
>>> nickel = mint.create(Nickel)
>>> nickel.year # The mint has not updated its stamp yet
2015
>>> nickel.worth() # 5 cents + (85 - 50 years)
40
>>> 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)
35
>>> Mint().create(Dime).worth() # A new mint has the current year
10
>>> dime.worth() # 10 cents + (160 - 50 years)
120
>>> Dime.cents = 20 # Upgrade all dimes!
>>> dime.worth() # 20 cents + (160 - 50 years)
130
"""
current_year = 2015
def __init__(self):
self.update()
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