Structure and Interpretation of Computer Programs

Similar documents
Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Midterm Exam 1 Solutions

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

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 2014 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 2015 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 Fall 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 Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs Spring 2014 Test 2

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Structure and Interpretation of Computer Programs

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

61A Lecture 9. Friday, September 20

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

Structure and Interpretation of Computer Programs Spring 2016 Test 1

ENVIRONMENT DIAGRAMS AND RECURSION 2

Control and Environments Fall 2017 Discussion 1: August 30, 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

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

Structure and Interpretation of Computer Programs

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

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

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

CS 1301 CS1 with Robots Summer 2007 Exam 1

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

DRAWING ENVIRONMENT DIAGRAMS

CS 1301 Exam 1 Fall 2010

Practice Midterm 1. Problem Points Score TOTAL 50

CONTROL AND ENVIRONMENTS 1

RLISTS AND THE ENVIRONMENT MODEL 9

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

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

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

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

C ONTROL AND H IGHER O RDER F UNCTIONS

Structure and Interpretation of Computer Programs

University of Massachusetts Lowell

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

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

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

COS 126 Midterm 1 Written Exam Fall 2012

FORM 2 (Please put your name and form # on the scantron!!!!)

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

CS 1301 Exam 1 Fall 2010

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

LINKED LISTS AND MIDTERM REVIEW

Control Flow: Loop Statements

Midterm Exam 2B Answer key

COS 126 Midterm 1 Written Exam Fall 2011

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

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

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 20, 2013

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

CS Prelim 2 Review Fall 2018

CS 61B Data Structures and Programming Methodology. June David Sun

CSE413 Midterm. Question Max Points Total 100

CONTROL AND HIGHER ORDER FUNCTIONS 1

Structure and Interpretation of Computer Programs

Practice Midterm 1 Answer Key

CS 1301 Exam 1 Spring 2011

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

CS171 Midterm Exam. October 29, Name:

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

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Transcription:

CS 6A Fall 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 8.5 crib sheet of your own creation and the official 6A 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 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. Q. Q. 3 Q. 4 Total / / /4 / /5

. ( points) The Call Express is Delayed For each of the following call expressions, write the value to which it evaluates and what would be output by the interactive Python interpreter. The first two rows have been provided as examples. Assume that you have started Python 3 and executed the following statements: from operator import add, mul square (x): return mul (x, x) delay ( arg ): print ( delayed ) g (): return arg return g Expression Evaluates to Interactive Output square(5) 5 5 / Error Error 6 print(square(4)) None square(square(print())) Error Error print(add(3, 4), print(5)) None 5 7 None delay(square)(3) Error delayed Error add(delay(square)()(), ) 5 delayed 5 delay(delay)()(6)() 6 delayed delayed 6

Login: 3. ( points) Protect the Environment (a) (6 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, labels, and parent annotations to all local frames. Add all missing values created during execution. Show the return value for each local frame. horse(mask): horse = mask mask(horse): return horse return horse(mask) Global frame horse mask func horse(mask) mask = lambda horse: horse() f: horse horse(mask) horse mask func λ(horse) λ func mask(horse) [parent=f] horse mask [parent=f] horse

4 (b) (6 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, labels, and parent annotations to all local frames. Add all missing values created during execution. Show the return value for each local frame. p, s, y =,, 3 Global frame gang(p): nam = style(p) return (nam(4), 5) style(s): return lambda y: (p, s, y) p s y gang 3 func gang(p) gang(3) style gang func style(s) p 3 nam f: style s 3 func λ(y) [parent=f] λ(y) [parent=f] y 4 5 3 4

Login: 5 3. (4 points) Sequences (a) ( pt) Fill in the blanks so that the final call expression below evaluates to a value. pair (x): if x == 3: return lambda : (,, 3) else : return lambda : 4 ( lambda, soda : hall )( pair, " sequence ") (lambda hall, soda: hall(3)())(pair, sequence ) (b) ( pt) Draw a box and pointer diagram for the following rlist: a = rlist (, rlist ( rlist (, (3, empty_rlist )), rlist ((4, 5, 6), empty_rlist ))) None 4 5 6 3 None (c) ( pt) What is the element at index of this rlist, returned by getitem_rlist(a, )? (, (3, None)) getitem_rlist (s, i): """ Return the element at index i of recursive list s. """ while i > : s, i = rest (s), i - return first (s) (d) ( pt) What is the length of this rlist, returned by len_rlist(a)? 3 len_rlist (s): """ Return the length of recursive list s. """ length = while s!= empty_ rlist : s, length = rest (s), length + return length

6 (e) (6 pt) When the int constructor is called on a float value, it truncates toward zero, meaning that it returns the largest integer less than any positive argument, or the least integer greater than any negative argument. For example: >>> int () >>> int (.7) >>> int ( -.5) - Assume that you have started Python 3 and executed the following statements: alt (f, g, z): while g(z) > and z!= 5: f, g = g, f z = g(z) return z grow (x): return int (( x * 3) / ) shrink (x): return x - flip (x): return int ( / (x -)) For each of the following call expressions, write the value to which it evaluates. If evaluation causes an error, write Error. If evaluation would run forever, write Forever. alt(grow, shrink, 3) alt(grow, shrink, 4) Forever alt(flip, shrink, 3)

Login: 7 4. ( points) In Verse The inverse of some function F is a function of argument X that returns you the Y, such that when you apply F to Y you recover the X. There once was a rhyming device That was built to make any sound, twice, But used orthography And not phonology To decide if a rhyme would suffice. An invertible function is a function that takes and returns a single numeric value, is differentiable, and never returns the same value for two different arguments. Some examples: double (y): """ Return twice the value of y. """ return * y cube (y): """ Return y raised to the third power. """ return pow (y, 3) pow (y): """ Return raised to the power of y. """ return pow (, y) (a) (4 pt) Implement a function invert that takes an invertible function argument and returns its inverse. You may call find root, newton update, approx deriv, and/or iter improve. You cannot use any assignment, conditional, while, or for statements. invert (f): """ Return the inverse of invertible function f. >>> halve = invert ( double ) >>> halve ( ) 6. >>> cube_ root = invert ( cube ) >>> cube_ root ( 7) 3. >>> log = invert ( pow ) >>> log ( 3) 5. """ g(x): return find_root ( lambda y: f(y) - x) return g

8 A sight rhyme is a pair of words that do not rhyme, but have the same endings, such as device and office. Two numbers that end in the same digit can be sight rhymes. For example: (3, 53) are pronounced thirteen and fifty-three, despite both ending with the same one s digit 3. (, 3) are pronounced zero and thirty, despite both ending with the same one s digit. (b) (4 pt) A numpair is a pair of integers that have the same one s digit. Fill in the two missing expressions in the constructor below, which takes two non-negative integers less than, asserts that they have the same one s digit, and returns a numpair represented as a pair of tens digits and the shared one s digit. from operator import floordiv, mod # Use these functions or // and % numpair ( first, second ): """ Return a numpair as a pair of ten s digits and a shared one s digit. >>> numpair (3, 53) ((, 5), 3) >>> numpair (67, 7) ((6, ), 7) """ assert first % == second % return (( first //, second // ), first % ) (c) (4 pt) Fill in four missing expressions below so that sight_rhyme returns whether the numbers in a numpair p do not end with the same sound when pronounced. Your implementation cannot depend on the representation of a numpair; use selector functions. You cannot use the boolean operators and or or. ones (p): return p [] first_tens (p): return p [][] second_tens (p): return p [][] sight_rhyme (p): """ Return whether the two numbers in a numpair do not rhyme. >>> sight_rhyme ( numpair (3, 53)) True >>> sight_rhyme ( numpair (, 3)) True >>> sight_rhyme ( numpair (53, 3)) False """ if ( first_tens (p) == )+( second_tens (p) == ) >= : return first_tens (p)!= second_tens (p) elif ones (p) == : if first_tens (p) == : return second_ tens ( p)!= else : return second_ tens ( p) == else : return False