Problem 4: Trictionary or Treat (200pts)
A trictionary is a pair of tree k
and v
.
They have identical structure: each node in k
has a corresponding node in v
.
The labels in k
are called keys. Each key may be the label for multiple nodes in k
,
and the values for that key are the labels of all the corresponding nodes in v
.
A lookup function returns one of the values for a key.
Specifically, a lookup function for a node in k
is a function that takes v
as an argument and
returns the label for the corresponding node in v
.
Implement the generator function lookups
, which takes as input a tree k
and a key.
It yields all lookup functions for nodes in k
that have key as their label, the functions could be yielded in any order.
def lookups(k, key):
"""Yield one lookup function for each node of k that has the label key.
>>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])])
>>> v = tree('Go', [tree('C', [tree('C')]), tree('A', [tree('S'), tree(6)]), tree('L', [tree(1), tree('A')])])
>>> type(lookups(k, 4))
<class 'generator'>
>>> sorted([f(v) for f in lookups(k, 2)])
['A', 'C']
>>> sorted([f(v) for f in lookups(k, 3)])
['S']
>>> [f(v) for f in lookups(k, 6)]
[]
"""
"*** YOUR CODE HERE ***"