Homework 3 Solutions
Solution Files
You can find the solutions in hw03.py.
Required Questions
Getting Started Videos
These videos may provide some helpful direction for tackling the coding problems on this assignment.
To see these videos, you should be logged into your berkeley.edu email.
Trees
Q1: Finding Berries!
The squirrels on campus need your help! There are a lot of trees on campus and
the squirrels would like to know which ones contain berries. Define the function
berry_finder
, which takes in a tree and returns True
if the tree contains a
node with the value 'berry'
and False
otherwise.
Hint: To iterate through each of the branches of a particular tree, you can consider using a
for
loop to get each branch.
def berry_finder(t):
"""Returns True if t contains a node with the value 'berry' and
False otherwise.
>>> scrat = tree('berry')
>>> berry_finder(scrat)
True
>>> sproul = tree('roots', [tree('branch1', [tree('leaf'), tree('berry')]), tree('branch2')])
>>> berry_finder(sproul)
True
>>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
>>> berry_finder(numbers)
False
>>> t = tree(1, [tree('berry',[tree('not berry')])])
>>> berry_finder(t)
True
"""
if label(t) == 'berry':
return True
for b in branches(t):
if berry_finder(b):
return True
return False
# Alternative solution
def berry_finder_alt(t):
if label(t) == 'berry':
return True
return True in [berry_finder(b) for b in branches(t)]
Use Ok to test your code:
python3 ok -q berry_finder
Q2: Sprout Leaves
Define a function sprout_leaves
that takes in a tree, t
, and a list of
leaves, leaves
. It produces a new tree that is identical to t
, but where each
old leaf node has new branches, one for each leaf in leaves
.
For example, say we have the tree t = tree(1, [tree(2), tree(3, [tree(4)])])
:
1
/ \
2 3
|
4
If we call sprout_leaves(t, [5, 6])
, the result is the following tree:
1
/ \
2 3
/ \ |
5 6 4
/ \
5 6
def sprout_leaves(t, leaves):
"""Sprout new leaves containing the labels in leaves at each leaf of
the original tree t and return the resulting tree.
>>> t1 = tree(1, [tree(2), tree(3)])
>>> print_tree(t1)
1
2
3
>>> new1 = sprout_leaves(t1, [4, 5])
>>> print_tree(new1)
1
2
4
5
3
4
5
>>> t2 = tree(1, [tree(2, [tree(3)])])
>>> print_tree(t2)
1
2
3
>>> new2 = sprout_leaves(t2, [6, 1, 2])
>>> print_tree(new2)
1
2
3
6
1
2
"""
if is_leaf(t):
return tree(label(t), [tree(leaf) for leaf in leaves])
return tree(label(t), [sprout_leaves(s, leaves) for s in branches(t)])
Use Ok to test your code:
python3 ok -q sprout_leaves
Iterators
Q3: Count Occurrences
Implement count_occurrences
, which takes an iterator t
, an integer n
, and
a value x
. It returns the number of elements in the
first n
elements of t
that are equal to x
.
You can assume that t
has at least n
elements.
Important: You should call
next
ont
exactlyn
times. If you need to iterate through more thann
elements, think about how you can optimize your solution.
def count_occurrences(t, n, x):
"""Return the number of times that x is equal to one of the
first n elements of iterator t.
>>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
>>> count_occurrences(s, 10, 9)
3
>>> t = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
>>> count_occurrences(t, 3, 10)
2
>>> u = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
>>> count_occurrences(u, 1, 3) # Only iterate over 3
1
>>> count_occurrences(u, 3, 2) # Only iterate over 2, 2, 2
3
>>> list(u) # Ensure that the iterator has advanced the right amount
[1, 2, 1, 4, 4, 5, 5, 5]
>>> v = iter([4, 1, 6, 6, 7, 7, 6, 6, 2, 2, 2, 5])
>>> count_occurrences(v, 6, 6)
2
"""
count = 0
for _ in range(n):
if next(t) == x:
count += 1
return count
Use Ok to test your code:
python3 ok -q count_occurrences
Generators
Q4: Merge
Definition: An infinite iterator is a iterator that never stops providing
values when next
is called. For example, ones()
evaluates to an infinite
iterator:
def ones():
while True:
yield 1
Write a generator function merge(a, b)
that takes two infinite iterators, a
and b
, as inputs. Both iterators yield elements in strictly increasing order with no duplicates. The generator should produce all unique elements from both input iterators in increasing order, ensuring no duplicates.
Note: The input iterators do not contain duplicates within themselves, but they may have common elements between them.
def merge(a, b):
"""
Return a generator that has all of the elements of generators a and b,
in increasing order, without duplicates.
>>> def sequence(start, step):
... while True:
... yield start
... start += step
>>> a = sequence(2, 3) # 2, 5, 8, 11, 14, ...
>>> b = sequence(3, 2) # 3, 5, 7, 9, 11, 13, 15, ...
>>> result = merge(a, b) # 2, 3, 5, 7, 8, 9, 11, 13, 14, 15
>>> [next(result) for _ in range(10)]
[2, 3, 5, 7, 8, 9, 11, 13, 14, 15]
"""
a_val, b_val = next(a), next(b)
while True:
if a_val == b_val:
yield a_val
a_val, b_val = next(a), next(b) elif a_val < b_val:
yield a_val
a_val = next(a) else:
yield b_val
b_val = next(b)
Use Ok to test your code:
python3 ok -q merge
Q5: Yield Paths
Write a generator function yield_paths
that takes a tree t
and a target value
. It yields each path from the root of t
to any node with the label value
.
Each path should be returned as a list of labels from the root to the matching node. The paths can be yielded in any order.
Hint: If you are having trouble getting started, think about how you would approach this problem if it was not a generator function. What would the recursive steps look like?
Hint: Remember, you can iterate over generator objects because they are a type of iterator!
def yield_paths(t, value):
"""
Yields all possible paths from the root of t to a node with the label
value as a list.
>>> t1 = tree(1, [tree(2, [tree(3), tree(4, [tree(6)]), tree(5)]), tree(5)])
>>> print_tree(t1)
1
2
3
4
6
5
5
>>> next(yield_paths(t1, 6))
[1, 2, 4, 6]
>>> path_to_5 = yield_paths(t1, 5)
>>> sorted(list(path_to_5))
[[1, 2, 5], [1, 5]]
>>> t2 = tree(0, [tree(2, [t1])])
>>> print_tree(t2)
0
2
1
2
3
4
6
5
5
>>> path_to_2 = yield_paths(t2, 2)
>>> sorted(list(path_to_2))
[[0, 2], [0, 2, 1, 2]]
"""
if label(t) == value:
yield [value] for b in branches(t):
for path in yield_paths(b, value): yield [label(t)] + path
Use Ok to test your code:
python3 ok -q yield_paths
If our current label is equal to value
, we've found a path from the root to a node
containing value
containing only our current label, so we should yield that. From there,
we'll see if there are any paths starting from one of our branches that ends at a
node containing value
. If we find these "partial paths" we can simply add our current
label to the beinning of a path to obtain a path starting from the root.
In order to do this, we'll create a generator for each of the branches which yields
these "partial paths". By calling yield_paths
on each of the branches, we'll create
exactly this generator! Then, since a generator is also an iterable, we can iterate over
the paths in this generator and yield the result of concatenating it with our current label.
Check Your Score Locally
You can locally check your score on each question of this assignment by running
python3 ok --score
This does NOT submit the assignment! When you are satisfied with your score, submit the assignment to Gradescope to receive credit for it.
Submit Assignment
Submit this assignment by uploading any files you've edited to the appropriate Gradescope assignment. Lab 00 has detailed instructions.
[Optional] Exam Practice
Homework assignments will also contain prior exam-level questions for you to take a look at. These questions have no submission component; feel free to attempt them if you'd like a challenge!
- Summer 2021 MT Q4: Maximum Exponen-tree-ation
- Summer 2019 MT Q8: Leaf It To Me
- Summer 2017 MT Q9: Temmie Flakes