Structure and Interpretation of Computer Programs

Similar documents
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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

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 Spring 2016 Test 1

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Structure and Interpretation of Computer Programs

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

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

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

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

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs

EXAM PREPARATION SECTION 1

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs Spring 2014 Test 2

Structure and Interpretation of Computer Programs

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

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

CS3 Midterm 2 Summer 2008

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

CONTROL AND ENVIRONMENTS 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

DRAWING ENVIRONMENT DIAGRAMS

Spring Semester 13, Dr. Punch. Exam #1 (2/14), form 1 A

Structure and Interpretation of Computer Programs

C ONTROL AND H IGHER O RDER F UNCTIONS

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

61A Lecture 2. Wednesday, September 4, 2013

ENVIRONMENT DIAGRAMS AND RECURSION 2

Question Points Score

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

CS 61A Discussion 8: Scheme. March 23, 2017

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

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

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

CS 1301 Exam 1 Fall 2010

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Read and fill in this page now

This was the second midterm from I have added a few comments in bold type, like this.

Structure and Interpretation of Computer Programs

61A Lecture 9. Friday, September 20

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

CS 1301 Exam 1 Fall 2010

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

61A Lecture 2. Friday, August 28, 2015

CS 1301 CS1 with Robots Summer 2007 Exam 1

Midterm II CS164, Spring 2006

CS 1301 Exam 1 Spring 2011

61A Lecture 3. Friday, September 5

CS 1301 Exam 1 Spring 2014

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

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS 61A HKN Review Session. Nathan Zhang, Caleb Wyllie

Introduction to Artificial Intelligence Midterm 1. CS 188 Spring You have approximately 2 hours.

CS 2316 Exam 4 Fall 2011

Transcription:

CS A Spring 05 Structure and Interpretation of Computer Programs Midterm Solutions INSTRUCTIONS You have hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except one hand-written.5 crib sheet of your own creation and the official A midterm study guide attached to the back of this exam. Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation. Last name First name SID Email (...@berkeley.edu) Login (e.g., csa-ta) 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. Q. Q. Total / /4 /4 /40

. ( points) In-N-Out For each of the expressions in the tables below, write the output displayed by the interactive Python interpreter when the expression is evaluated. The output may have multiple lines. No errors occur. The first two rows have been provided as examples of the behavior of the built-in pow and print functions. Recall: The interactive interpreter displays the value of a successfully evaluated expression, unless it is. Assume that you have started Python and executed the following statements: from operator import add def re( peat ): print ( peat, peat ) def cheap ( eat ): car, seat = re, print seat ( car ( eat )) double ( eat ) def double ( double ): if double : double + double elif car ( double )( print )( print ): 000 else : seat () seat = double car = lambda c: lambda a: lambda r: r(5, a(c)) Expression Interactive Output pow(, ) print(4, 5) 4 5 print(re(+), print(4)) 4 cheap() cheap(seat()) 4 4 Expression car()(double)(pow) double(print()) car(0)(seat)(add) Interactive Output 5 5 0 5

Login:. (4 points) Supernatural (a) ( pt) 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 or frames. 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 value for each local frame. Remember: Do not add a new frame when calling a built-in function (such as abs). 4 5 7 9 0 4 batman, superman, ivy =, -, - def nanana(batman): while batman(superman) > ivy: def batman(joker): ivy -ivy def joker(superman): if superman(batman): ivy = -batman nanana joker(abs)(abs) Global frame batman superman - ivy - joker nanana f: joker [parent=global] func joker(superman) [parent=global] func nanana(batman) [parent=global] superman func abs(...) [parent=global] ivy - f: nanana [parent=global] batman func batman(joker) [parent=f] f: batman [parent=f] joker - -

4 (b) ( pt) 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 or frames. 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 value for each local frame. 4 5 7 9 def still(glad): def heart(broken): glad = lambda heart: lambda: heart-broken glad(grin) heart(glad-grin)() broken, grin = 5, still(broken-) Global frame still broken grin 5 func still(glad) [parent=global] f: still [parent=global] glad 4 heart func heart(broken) [parent=f] f: heart [parent=f] broken glad func λ(heart) [parent=f] f: λ [parent=f] heart func λ() [parent=f] f4: λ [parent=f]

Login: 5. (4 points) You Complete Me (a) (4 pt) Implement the longest_increasing_suffix function, which s the longest suffix (end) of a positive integer that consists of strictly increasing digits. def longest_increasing_suffix (n): """ Return the longest increasing suffix of a positive integer n. >>> longest_increasing_suffix (4) 4 >>> longest_increasing_suffix () >>> longest_increasing_suffix (59) 59 >>> longest_increasing_suffix (590) # 0 is the suffix, displayed as """ m, suffix, k = 0, 0, while n: n, last = n // 0, n % 0 if last < m: else : m, suffix, k = last, suffix + k * last, 0 * k suffix suffix (b) ( pt) Add parentheses and single-digit integers in the blanks below so that the expression on the second line evaluates to 05. You may only add parentheses and single-digit integers. You may leave some blanks empty. lamb = lambda lamb : lambda : lamb + lamb lamb (000)() + ( lambda b, c: b ()* b() -c)( lamb (), )

(c) ( pt) Implement the combine function, which takes a non-negative integer n, a two-argument function f, and a number result. It applies f to the first digit of n and the result of combining the rest of the digits of n by repeatedly applying f (see the doctests). If n has no digits (because it is zero), combine s result. from operator import add, mul def combine (n, f, result ): """ Combine the digits in non - negative integer n using f. >>> combine (, mul, ) # mul (, ) >>> combine (4, mul, ) # mul (4, mul (, )) 4 >>> combine (50, add, ) # add (, add (5, add (0, add (, ))) >>> combine (9, pow, 0) # pow (, pow (, pow (9, 0))) """ if n == 0: result else : combine (n // 0, f, f(n % 0, result )) (d) (4 pt) Implement the memory function, which takes a number x and a single-argument function f. It s a function with a peculiar behavior that you must discover from the doctests. You may only use names and call expressions in your solution. You may not write numbers or use features of Python not yet covered in the course. square = lambda x: x * x double = lambda x: * x def memory (x, f): """ Return a higher - order function that prints its memories. >>> f = memory (, lambda x: x) >>> f = f( square ) >>> f = f( double ) 9 >>> f = f( print ) >>> f = f( square ) """ def g(h): print (f(x)) memory (x, h) g