CS 61A Structure and Interpretation of Computer Programs Fall 2017
CS 61A Structure and Interpretation of Computer Programs Fall 2017
INSTRUCTIONS
Last name
First name
Student ID number
TA
def alex(hamil):
def g(w):
hamil = 2 * w
print(hamil, w) alex(3)
w = hamil
return hamil
w = 5
alex = g(w + 1)
print(w, alex, hamil)
el(3, el)
def el(i, za):
def angelica():
return i + 1
if i > 10:
return za()
elif i > 4: K
print(angelica())
return el(i * i, za)
else: K(3)
return el(i * i,
angelica)
K(3)(2)
K = lambda x: lambda y: x
pr(True) and pr(0) and pr(1)
def pr(x):
print(x)
return x
Name: 3
• Add all missing names and parent annotations to all local frames.
• Add all missing values created or referenced during execution.
• Show the return value for each local frame.
1 x = 1 Global frame
2 def f(n):
3 def g(): func f(n) [parent=Global]
4 return n + x
5 x = n + 5
6 if n % 3 == 0:
7 return g func [parent= ]
8 else:
9 return f(n + 1)
10 x = 10 f1: [parent= ]
11 func [parent= ]
z = f(2)
12 q = x + z()
func [parent= ]
f2: [parent= ]
Return value
f3: [parent= ]
Return value
4
3. (3 points) Triangulate
It’s easy to see that in any triangle, each side must be shorter than the sum of the other two. Implement
triangle, which takes three positive numbers, a, b, and c, and returns whether these three numbers could
possibly be the lengths of the three sides of a triangle.
def triangle(a, b, c):
"""Return whether a, b, and c could be the legs of a triangle.
>>> triangle(3, 4, 5)
True
>>> triangle(3, 4, 6)
True
>>> triangle(6, 3, 4)
True
>>> triangle(3, 6, 4)
True
>>> triangle(9, 2, 2)
False
>>> triangle(2, 4, 2)
False
"""
longest = ______________________________________________________________________________
sum_of_others = ________________________________________________________________________
4. (9 points) Digital
(a) (3 pt) Implement collapse, which takes a non-negative integer, and returns the result of removing all
digits from it that duplicate the digit immediately to their right.
def collapse(n):
"""For non-negative N, the result of removing all digits that are equal
to the digit on their right, so that no adjacent digits are the same.
>>> collapse(1234)
1234
>>> collapse(12234441)
12341
>>> collapse(0)
0
>>> collapse(3)
3
>>> collapse(11200000013333)
12013
"""
if ___________________________________________________________________________________:
return last
return collapse(__________________________________________________________________)
else:
(b) (6 pt) Implement find_pair, which takes a two-argument function, p, as input and returns another function.
The returned function takes a non-negative integer n; it returns True if and only if p returns a true value
when called on at least one pair of adjacent digits in n, and False otherwise.
def find_pair(p):
"""Given a two-argument function P, return a function that takes a
non-negative integer and returns True if and only if two adjacent digits
in that integer satisfy P (that is, cause P to return a true value).
def find(n):
while ___________________________________________________________________________:
if __________________________________________________________________________:
return __________________________________________________________________
else:
_______________ = _______________________________________________________
_________________________________________________________________________________
_____________________________________________________________________________________
Name: 7
(a) (5 pt) Implement confirmer so that when confirmer takes a positive integer code, it returns a confirming
function for the digits of that code.
def confirmer(code):
"""Return a confirming function for CODE.
if ______________________________________________________________________________:
return ______________________________________________________________________
else:
return ______________________________________________________________________
return ______________________________________________________________________________
8
(b) (5 pt) Given a confirming function, one can find the code it confirms, one digit at a time. Implement decode,
which takes a confirming function f and returns the code that it confirms.
def decode(f, y=0):
"""Return the code for a confirming function f.
>>> decode(confirmer(12001))
12001
>>> decode(confirmer(56789))
56789
"""
d = 0
if x == True:
return code
elif x == False:
______________________________________________________________________________
else:
______________________________________________________________________________