0% found this document useful (0 votes)
7 views8 pages

CS 61A Structure and Interpretation of Computer Programs Fall 2017

The document is a midterm exam for CS 61A, focusing on the Structure and Interpretation of Computer Programs. It includes instructions for the exam format, several coding problems requiring Python programming skills, and tasks related to environmental diagrams and function implementations. Students are expected to complete the exam within two hours using a crib sheet and the official study guide.

Uploaded by

matuyiyay
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)
7 views8 pages

CS 61A Structure and Interpretation of Computer Programs Fall 2017

The document is a midterm exam for CS 61A, focusing on the Structure and Interpretation of Computer Programs. It includes instructions for the exam format, several coding problems requiring Python programming skills, and tasks related to environmental diagrams and function implementations. Students are expected to complete the exam within two hours using a crib sheet and the official study guide.

Uploaded by

matuyiyay
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/ 8

CS 61A Structure and Interpretation of Computer Programs

Fall 2017 Midterm 1

INSTRUCTIONS

• You have 2 hours to complete the exam.


• The exam is closed book, closed notes, closed computer, closed calculator, except one hand-written 8.5" × 11"
crib sheet of your own creation and the official CS 61A midterm 1 study guide.
• Mark your answers on the exam itself. We will not grade answers written on scratch paper.

Last name

First name

Student ID number

CalCentral email (_@berkeley.edu)

TA

Name of the person to your left

Name of the person to your right

All the work on this exam is my own.


(please sign)
2

1. (10 points) I Wonder What Python Would Display


For each of the expressions in the table below, write the output displayed by the interactive Python interpreter
when the expression is evaluated. The output may have multiple lines. If an error occurs, write “Error”, but
include all output displayed before the error. To display a function value, write “Function”. The first two rows
have been provided as examples.
The interactive interpreter displays the value of a successfully evaluated expression, unless it is None.
Assume that you have first started python3 and executed the statements on the left.

Expression Interactive Output


pow(2, 3) 8
aaron, burr = 2, 5 45
print(4, 5) + 1
aaron, burr = 4, aaron + 1 Error
print(aaron, burr)
hamil = 10

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

2. (8 points) Environmental Influences


Fill in the environment diagram that results from executing the code below until the entire program is finished,
an error occurs, or all frames are filled. You may not need to use all of the spaces, frames, or function values.
A complete answer will:

• 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= ]

Return value 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 = ________________________________________________________________________

return longest ___________________________________________________________ sum_of_others


Name: 5

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

left, last = n // 10, n % 10

if ___________________________________________________________________________________:

return last

elif _________________ == ____________________________________________________________:

return collapse(__________________________________________________________________)

else:

return collapse(___________________________) * 10 + ______________________________


6

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

>>> z = find_pair(lambda a, b: a == b) # Adjacent equal digits


>>> z(1313)
False
>>> z(12334)
True
>>> z = find_pair(lambda a, b: a > b)
>>> z(1234)
False
>>> z(123412)
True
>>> find_pair(lambda a, b: a <= b)(9753)
False
>>> find_pair(lambda a, b: a == 1)(1) # Only one digit; no pairs.
False
"""

def find(n):

while ___________________________________________________________________________:

if __________________________________________________________________________:

return __________________________________________________________________

else:

_______________ = _______________________________________________________

_________________________________________________________________________________

_____________________________________________________________________________________
Name: 7

5. (10 points) Please Confirm


Definition. A confirming function for a sequence of digits, called a code, takes a single digit as its only
argument. If the digit does not match the first (left-most) digit of the code to be confirmed, it returns False.
If the digit does match, then the confirming function returns True if the code has only one digit, or another
confirming function for the rest of the code if there are more digits to confirm.

(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.

>>> confirmer(204)(2)(0)(4) # The digits of 204 are 2, then 0, then 4.


True
>>> confirmer(204)(2)(0)(0) # The third digit of 204 is not 0.
False
>>> confirmer(204)(2)(1) # The second digit of 204 is not 1.
False
>>> confirmer(204)(20) # The first digit of 204 is not 20.
False
"""
def confirm1(d, t):
def result(digit):
if d == digit:
return t
else:
return False
return result

def extend(prefix, rest):


"""Return a confirming function that returns REST when given the digits of PREFIX.
For example, if c = extend(12, confirmer(34)), then c(1)(2) returns confirmer(34),
so that c is a confirming function for 1234."""

left, last = prefix // 10, prefix % 10

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

while d < 10:

x, code = _________________________________ , _____________________________________

if x == True:

return code

elif x == False:

______________________________________________________________________________

else:

______________________________________________________________________________

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy