0% found this document useful (0 votes)
14 views4 pages

37-Final Examples 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

37-Final Examples 4pp

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Final Examples Announcements

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 ☑

>>> a = Tree(1, [Tree(4, [Tree(4), Tree(5)]), Tree(3, [Tree(0, [Tree(2)])])]) 3 ☑


Tree Processing >>> bigs(a)
4 4 ☑
0

4 5 ☑ 2

if t.is_leaf(): Somehow track a


return ___ list of ancestors
else:
if node.label > max(ancestors):
return ___([___ for b in t.branches])
Somehow track the
Somehow increment largest ancestor
the total count
if node.label > max_ancestors:
6

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.

>>> a = Tree(1, [Tree(4, [Tree(4), Tree(5)]), Tree(3, [Tree(0, [Tree(2)])])])


>>> bigs(a)
Somehow track the f( ,0)
Recursive Accumulation
4
""" largest ancestor 1 ☑
def f(a, x): f( ,1)
A node max_ancestor node.label > max_ancestors
a.label > x f( ,1) 3 ☑
if _____________________________________________________:
f( ,3)
4 ☑
sum([f(b, a.label) for b in a.branches])
return 1 + _________________________________________ 0
f( ,3)
Somehow increment the total count
else: 4 5 ☑ 2
sum([ f(b, x) for b in a.branches] ) f( ,4) f( ,4)
return _____________________________________________
f(t, t.label - 1 ) Root label is always larger than its ancestors
return _____________________________________________________
Some initial value for the largest ancestor so far... 7
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 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]

How to Design Programs

From Problem Analysis to Data Definitions


Identify the information that must be represented and how it is represented in the chosen
programming language. Formulate data definitions and illustrate them with examples.

Signature, Purpose Statement, Header


State what kind of data the desired function consumes and produces. Formulate a concise
answer to the question what the function computes. Define a stub that lives up to the
signature.
Applying the Design Process
Functional Examples
Work through examples that illustrate the function’s purpose.

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))

Interpreters (define (f y) (+ x y))

(f (if (> 3 2) 4 5))

16

You might also like