37-Final Examples 4pp
37-Final Examples 4pp
Tree-Structured Data
def tree(label, branches=[]): A tree can contains other trees:
return [label] + list(branches)
[5, [6, 7], 8, [[9], 10]]
def label(t):
return t[0]
(+ 5 (- 6 7) 8 (* (- 9) 10))
def branches(t):
return t[1:] (S
(NP (JJ Short) (NNS cuts))
Trees (VP (VBP make)
def is_leaf(t):
return not branches(t) (NP (JJ long) (NNS delays)))
(. .))
class Tree:
def __init__(self, label, branches=[]): <ul>
self.label = label <li>Midterm <b>1</b></li>
self.branches = list(branches) <li>Midterm <b>2</b></li>
</ul>
def is_leaf(self):
return not self.branches Tree processing often involves
recursive calls on subtrees
4
Solving Tree Problems
Implement bigs, which takes a Tree instance t containing integer labels. It returns the
number of nodes in t whose labels are larger than all labels of their ancestor nodes.
(Assume the root label is always larger than all of its ancestors, since it has none.)
def bigs(t):
"""Return the number of nodes in t that are larger than all their ancestors. 1 ☑
4 5 ☑ 2
Implement bigs, which takes a Tree instance t containing integer labels. It returns the
number of nodes in t whose labels are larger than all labels of their ancestor nodes.
(Assume the root label is always larger than all of its ancestors, since it has none.)
def bigs(t):
"""Return the number of nodes in t that are larger than all their ancestors.
Implement bigs, which takes a Tree instance t containing integer labels. It returns the
number of nodes in t whose labels are larger than any labels of their ancestor nodes.
def bigs(t):
"""Return the number of nodes in t that are larger than all their ancestors."""
n = [0]
Somehow track the
def f(a, x): largest ancestor Designing Functions
node.label > max_ancestors
if a.label > x
________________________:
n[0] += 1
________________________ Somehow increment the total count
for b in a.branches
___________________________:
b, max(a.label, x)
f(_____________________)
f(t, t.label - 1) Root label is always larger than its ancestors
_______________________________
return n[0]
Function Template
Translate the data definitions into an outline of the function.
Function Definition
Fill in the gaps in the function template. Exploit the purpose statement and the examples.
Testing
Articulate the examples as tests and ensure that the function passes all. Doing so
discovers mistakes. Tests also supplement examples in that they help others read and
understand the definition when the need arises—and it will arise for any serious program.
11
https://htdp.org/2018-01-06/Book/
Designing a Function Designing a Function
Implement smalls, which takes a Tree instance t containing integer labels. It returns the Implement smalls, which takes a Tree instance t containing integer labels. It returns the
non-leaf nodes in t whose labels are smaller than any labels of their descendant nodes. non-leaf nodes in t whose labels are smaller than any labels of their descendant nodes.
Signature: Tree -> List of Trees Signature: Tree -> List of Trees
def smalls(t): def smalls(t):
"""Return the non-leaf nodes in t that are smaller than all their descendants. 1 """Return a list of the non-leaf nodes in t that are smaller than all descendants. 1
>>> a = Tree(1, [Tree(2, [Tree(4), Tree(5)]), Tree(3, [Tree(0, [Tree(6)])])]) >>> a = Tree(1, [Tree(2, [Tree(4), Tree(5)]), Tree(3, [Tree(0, [Tree(6)])])])
>>> sorted([t.label for t in smalls(a)]) 3 >>> sorted([t.label for t in smalls(a)]) 3
[0, 2] [0, 2]
2 ☑ 2 ☑
""" 0 ☑ """ 0 ☑
result = [] Signature: Tree -> number result = [] Signature: Tree -> number
def process(t): "Find smallest label in t & maybe add t to result" 4 5 6 def process(t): "Find smallest label in t & maybe add t to result" 4 5 6
if t.is_leaf(): if t.is_leaf():
t.label
return t.label return __________________________________________
else: else:
min([process(b) for b in t.branches])
smallest = ______________________________________
2 smallest label if ______________________________________________:
t.label < smallest 2
0 in a branch of t
0
result.append( t )
_____________________________________________
return min(...)
process(t)
[ 4 5 , 6 ] return min(smallest, t.label)
process(t)
[ 4 5 , 6 ]
return result return result
13 14
Interpreter Analysis
What expressions are passed to scheme_eval when evaluating the following expressions?
(define x (+ 1 2))
16