Midterm Exam 1 Solutions

Similar documents
Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Final Exam Solutions. Structure and Interpretation of Computer Programs. Fall 2011 /12 /16 /12 /18 /10 /12 /80 INSTRUCTIONS

61A Lecture 3. Friday, September 5

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CONTROL AND HIGHER ORDER FUNCTIONS 1

Structure and Interpretation of Computer Programs

61A Lecture 2. Wednesday, September 4, 2013

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

61A Lecture 6. Friday, September 7

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

61A Lecture 2. Friday, August 28, 2015

Structure and Interpretation of Computer Programs

ENVIRONMENT DIAGRAMS AND RECURSION 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CSE331 Winter 2014, Midterm Examination February 12, 2014

ITERATION AND RECURSION 3

Introduction to Fall 2008 Artificial Intelligence Midterm Exam

CSE331 Winter 2014, Midterm Examination February 12, 2014

Structure and Interpretation of Computer Programs

CONTROL AND ENVIRONMENTS 1

CONTROL AND HIGHER ORDER FUNCTIONS 2

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

C ONTROL AND H IGHER O RDER F UNCTIONS

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

An introduction to Scheme

CS150 Sample Final. Name: Section: A / B

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs

Lecture #3: Environments

Midterm Exam 2B Answer key

Control Flow: Loop Statements

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Structure and Interpretation of Computer Programs Spring 2016 Test 1

CSCE 110: Programming I

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs Spring 2014 Test 2

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

ˆ The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

All program statements you write should be syntactically correct. Partial credit is not guaranteed with incorrect use of syntax.

CSE413 Midterm. Question Max Points Total 100

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS 1110 Prelim 1 October 4th, 2012

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Spring 2011

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CS 1301 Exam 1 Fall 2010

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Lecture #6: Higher-Order Functions at Work

CS150 Sample Final Solution

Structure and Interpretation of Computer Programs

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 1301 CS1 with Robots Summer 2007 Exam 1

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 111X - Fall Test 1

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

CS 1301 Exam 1 Fall 2009

Structure and Interpretation of Computer Programs

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Fall 2014

: Intro Programming for Scientists and Engineers Final Exam

Transcription:

CS 61A Fall 2011 Structure and Interpretation of Computer Programs Midterm Exam 1 Solutions INSTRUCTIONS You have 2 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except a one-page crib sheet of your own creation and the official 61A Midterm 1 Study Guide. Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation. All short answer sections can be successfully answered in a few sentences at most. Last Name First Name SID Login TA & Section Time 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) For staff use only Q. 1 Q. 2 Q. 3 Q. 4 Total /10 /16 /14 /10 /50

2 THIS PAGE INTENTIONALLY LEFT BLANK

Login: 3 1. (10 points) Call Expressions Assume that you have started Python 3 and executed the following statements: from operator import add, mul def square(x): return mul(x, x) def curry(f): def g(x): return lambda y: f(x, y) return g For each of the following call expressions, write the value to which it evaluates in the current environment (which may differ from what is printed). If evaluating the call expression causes an error, write error and the reason for the error. (a) (2 pt) add(4, square(print(2))) Error: print(2) evaluates to None. Cannot multiply None and None in the body of square. (b) (2 pt) add(4, square(square(2))) 20 (c) (2 pt) print(4, square(square(2))) None (d) (2 pt) curry(add)(square(2))(4) 8 (e) (2 pt) add(curry(square)(2), 4) Error: Cannot add a function and an int.

4 2. (16 points) Defining Functions (a) (5 pt) A function streak returns the greatest number of 1 s returned consecutively in n calls to coin. Its second argument coin is a random, non-pure function that returns 1 sometimes and 0 other times. For instance, streak would return 3, if 10 calls to coin returned these 10 values, in order: 0, 1, 1, 0, 1, 1, 1, 0, 0, 1 Your partner hands you the following implementation of streak moments before this question is due. You don t have time to rewrite it; you can only indent lines, dedent (un-indent) lines, and delete things. Mark changes to the code below so that it correctly implements streak, using the following symbols: Place a right arrow ( ) before a line to indent it by an additional 4 spaces. Place a left arrow ( ) before a line to dedent it by 4 spaces. Circle a part of the code to delete it. def streak(n, coin): """Return the greatest number of 1 s returned consecutively in n calls to coin. coin -- A function that takes zero arguments and returns 0 or 1. n -- A positive integer """ n, k = 0, 0, 1 total = 0 # consecutive 1 s since a 0 greatest = 0 # The greatest number of consecutive 1 s while k <= n and coin() == 1: if coin == 0 or coin(n) == 0 or coin() == 0: This circle is optional greatest, total = max(greatest, total), total, 0 else: total = total + 1 k = k + 1 n = n + 1 return max(greatest, total) 6

Login: 5 (b) (5 pt) In Newton s method, the function iter improve may cycle between two different guesses. For instance, consider finding the zero of f(x) = x 3 2x + 2 (graphed below) using exact derivatives. A starting guess of 1 updates to 0, which updates to 1 again. The cyclic pattern 1, 0, 1 is called a length-two cycle, because it contains only two distinct elements before repeating. 4 3.2 2.4 1.6 0.8 2-1.5-1 -0.5 0 0.5 1 1.5 2-0.8 Write a new version of iter improve that returns False as soon as a length-two cycle of guesses occurs, but otherwise behaves the same as the implementation from class (and printed on your study guide). def iter_improve(update, done, guess=1, max_updates=1000): k = 0 previous_guess = None while not done(guess) and k < max_updates: next_guess = update(guess) if previous_guess == next_guess: return False previous_guess, guess = guess, next_guess k = k + 1 return guess

6 (c) (4 pt) The figure below depicts an example piecewise function, f(x) = { x 2 x < 1 3 x x 1 3 2 1 2-1.5-1 -0.5 0 0.5 1 1.5 2-1 Define a Python function called piecewise that takes three arguments: two functions lower func and upper func, and a number cutoff. lower func and upper func each take a single number argument and return a single number value. piecewise returns a new function that takes a single number argument x and returns lower func(x) if x is less than cutoff, or upper func(x) if x is greater than or equal to cutoff. For example: >>> f = piecewise(lambda x: x*x, lambda x: 3-x, 1) >>> f(0.5) 0.25 >>> f(5) -2 def piecewise(lower_func, upper_func, cutoff): def f(x): if x < cutoff: return lower_func(x) else: return upper_func(x) return f (d) (2 pt) Assume that you have started Python 3 and executed the following statement: def self_apply(f): return f(f) Write an expression in the blank operand position below, so that this call to self apply evaluates to 5: self_apply( lambda f: 5 )

Login: 7 3. (14 points) Environments (a) (6 pt) Fill in this environment diagram and expression tree that result from executing the Python code in the lower right of the page. A complete answer will: Complete all missing arrows. Add all local frames created by applying user-defined functions. Add all missing names and values. Add all missing statements, expressions, and return values in the expression tree. Turtles Solution mutant: ninja: turtle: y: 5 mutant(y): y, x = y+1, y+2 return ninja(y)/2 ninja(x): return x + 2 x: 6 turtle y: 6 x: 7 mutant turtle(x) return mul(x, y) + 2 mutant(y) y, x = y+1, y+2 return ninja(y)/2 32 16 from operator import mul def mutant(y): y, x = y+1, y+2 return ninja(y)/2 def ninja(x): return x + 2 def turtle(x): return mul(x, y) + 2 ninja(y) return mul(x, y) + 2 y, ninja = 5, turtle mutant(y)

8 (b) (6 pt) Fill in this diagram of environments and values that result from executing the Python code in the box below. You do not need to draw an expression tree. A complete answer will: Chuck Solution Complete all missing arrows. Add all local frames created by applying user-defined functions. Add all missing names and values. x: 10 walker: ranger: texas: walker(x): def texas(ranger): return x + 3 return texas n: 2 ranger(n): return x + walker(n)(n) ranger x: 2 texas: walker ranger: 2 texas texas(ranger): return x + 3 x = 10 def walker(x): def texas(ranger): return x + 3 return texas def ranger(n): return x + walker(n)(n) texas = ranger ranger(2) (c) (2 pt) What is the value of the nested expression texas(ranger(2)), evaluated in the current environment that results from executing the code in the previous problem? If evaluating this expression causes an error, write error and the reason for the error. 28

Login: 9 mount Solution 4. (10 points) Data Abstraction An amount is a collection of nickels (5 cents) and pennies (1 cent), with the following behavior condition: If an amount a is constructed with n nickels and p pennies, then 5 nickels(a) + pennies(a) = 5 n + p. Consider the following implementation of an abstract data type for amounts. def make_amount(n, p): return (n, p) def nickels(a): return a[0] def pennies(a): return a[1] (a) (2 pt) Circle all abstraction barrier violations for this abstract data type in the following implementation of add amounts, if there are any. def add_amounts(a1, a2): n1 = nickels(a1) n2 = nickels(a2) n = n1 + n2 p = a1[1] + a2[1] return (n, p) (b) (4 pt) An amount is minimal if it has no more than 4 pennies. Redefine the constructor so that all amounts are minimal, but the behavior condition of the type still holds. def make_amount(n, p): return (n + p // 5, p % 5) 7

10 (c) (4 pt) Suppose we redefine make amount functionally, as follows: def make_amount(n, p): def dispatch(message): if message == 0: return n elif message == 1: return p return dispatch Redefine the selectors to implement a valid abstract data type for amounts using this constructor, in which all amounts are minimal. That is, pennies should never return a number greater than 4. def nickels(a): return a(0) + a(1) // 5 def pennies(a): return a(1) % 5