Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

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 Fall 2016 Midterm 2

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

EXAM PREPARATION SECTION 1

Structure and Interpretation of Computer Programs Spring 2016 Test 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 Summer 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 Spring 2017 Mock 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 Spring 2019 Midterm 2

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Midterm Exam 1 Solutions

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

CS 2316 Exam 4 Fall 2011

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

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

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

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

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Structure and Interpretation of Computer Programs

CSE413 Midterm. Question Max Points Total 100

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

CONTROL AND HIGHER ORDER FUNCTIONS 2

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

CS2500 Exam 2 Fall 2014

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

CS 111X - Fall Test 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

C ONTROL AND H IGHER O RDER F UNCTIONS

CS 2316 Exam 4 Fall 2011

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

CS1800 Discrete Structures Final Version A

Midterm #2a Fall minutes

CONTROL AND HIGHER ORDER FUNCTIONS 1

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Midterm #2a Fall minutes DO NOT WRITE IN THIS AREA

Grade 6 Integers. Answer the questions. Choose correct answer(s) from the given choices. For more such worksheets visit

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

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

CONTROL AND ENVIRONMENTS 1

CS 1301 Exam 1 Fall 2010

Midterm #2a Spring minutes

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

Midterm #1a Fall minutes

Discrete Mathematics and Probability Theory Fall 2015 Rao Midterm 1

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Final Exam Version A

Midterm #1 Fall minutes

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

Midterm Practice Exam Sample Solutions

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

CS115 - Module 10 - General Trees

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

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Midterm Exam 2B Answer key

Midterm 1. CS Intermediate Data Structures and Algorithms. October 23, 2013

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Walt Whitman High School SUMMER REVIEW PACKET. For students entering AP CALCULUS BC

Structure and Interpretation of Computer Programs

Page 1 of 11 Name/SID: SOLUTIONS UNIVERSITY OF CALIFORNIA COLLEGE OF ENGINEERING

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

Transcription:

CS 61A Structure and Interpretation of Computer Programs Fall 2015 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 BearFacts 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. (12 points) Evaluators Gonna Evaluate 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. Hint: No answer requires more than 5 lines. (It s possible that all of them require even fewer.) The first two rows have been provided as examples. Recall: The interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have started python3 and executed the following statements: jazz ( hands ): if hands < out : hands * 5 else : jazz ( hands // 2) + 1 twist ( shout, it, out =7): while shout : shout, out = it( shout ), print ( shout, out ) lambda out : print ( shout, out ) hands, out = 2, 3 Expression Interactive Output pow(2, 3) 8 print(4, 5) + 1 4 5 Error print(none, print(none)) jazz(5) (lambda out: jazz(8))(9) twist(2, lambda x: x-2)(4) twist(5, print)(out) twist(6, lambda hands: hands-out, 2)(-1)

Name: 3 2. (12 points) Environmental Policy (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 and parent annotations to all local frames. Add all missing values created or referenced during execution. Show the value for each local frame. 1 2 3 4 5 6 7 8 9 10 11 12 13 the(donald): donald + 5 clin(ton): the(race): donald + 6 ton(ga): donald = ga-1 the(4)-3 ton donald, duck = 2, clin(the) duck = duck(8) Global frame the clin f1: [parent= ] func the(donald) [parent=global] func clin(ton) [parent=global] f2: [parent= ] f3: [parent= ]

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. The <line...> annotation in a lambda value gives the line in the Python source of a lambda expression. A complete answer will: Add all missing names and parent annotations to all local frames. Add all missing values created or referenced during execution. Add all missing parents of function values. Show the value for each local frame. 1 2 3 4 5 6 7 8 inside(out): anger = lambda fear: fear(disgust) fear = lambda disgust: anger(out) disgust = 3 fear(5) fear, disgust = 2, 4 inside(lambda fear: fear + disgust) Global frame inside fear disgust func inside(out) [parent=global] f1: inside [parent=global] func λ(fear) <line 2> [parent= ] func λ(disgust) <line 3> [parent= ] func λ(fear) <line 8> [parent= ] f2: [parent= ] f3: [parent= ] f4: [parent= ]

Name: 5 3. (10 points) Digit Fidget **IMPORTANT DEFINITION** Each digit in a non-negative integer n has a digit position. Digit positions begin at 0 and count from the right-most digit of n. For example, in 568789, the digit 9 is at position 0 and digit 7 is at position 2. The digit 8 appears at both positions 1 and 3. (a) (3 pt) Implement the find_digit function, which takes a non-negative integer n and a digit d greater than 0 and less than 10. It s the largest (left-most) position in n at which digit d appears. If d does not appear in n, then find_digit s False. You may not use recursive calls. find_digit (n, d): Return the largest digit position in n for which d is the digit. >>> find_ digit (567, 7) 0 >>> find_ digit (567, 5) 2 >>> find_ digit (567, 9) False >>> find_ digit ( 568789, 8) # 8 appears at positions 1 and 3 3 i, k = 0, while n: n, last = n // 10, n % 10 if last == : i = i + 1 (b) (2 pt) Circle all values of y for which the final expression below evaluates to True. Assume that find_digit is implemented correctly. The compose1 function appears on the left column of page 2 of your study guide. 1 2 3 4 5 6 7 8 9 f = lambda x: find_digit (234567, x) compose1 (f, f)(y) == y

6 (c) (3 pt) Implement luhn_sum. The Luhn sum of a non-negative integer n adds the sum of each digit in an even position to the sum of doubling each digit in an odd position. If doubling an odd digit results in a two-digit number, those two digits are summed to form a single digit. You may not use recursive calls or call find_digit in your solution. luhn_sum (n): Return the Luhn sum of n. >>> luhn_ sum ( 135) # 1 + 6 + 5 12 >>> luhn_ sum ( 185) # 1 + ( 1+6) + 5 13 >>> luhn_ sum ( 138743) # From lecture : 2 + 3 + ( 1+6) + 7 + 8 + 3 30 luhn_ digit ( digit ): x = digit * (x // 10) + total, multiplier = 0, 1 while n: n, last = n // 10, n % 10 total = total + luhn_ digit ( last ) multiplier = - multiplier total (d) (2 pt) A non-negative integer has a valid Luhn sum if its Luhn sum is a multiple of 10. Implement check_digit, which appends one additional digit to the end of its argument so that the result has a valid Luhn sum. Assume that luhn_sum is implemented correctly. check_digit (n): Add a digit to the end of n so that the result has a valid Luhn sum. >>> check_ digit ( 153) # 2 + 5 + 6 + 7 = 20 1537 >>> check_ digit ( 13874) 138743

Name: 7 4. (6 points) Zombies! **IMPORTANT** In this question, assume that all of f, g, and h are functions that take one non-negative integer argument and a non-negative integer. You do not need to consider negative or fractional numbers. (a) (4 pt) Implement the higher-order function decompose1, which takes two functions f and h as arguments. It s a function g that relates f to h in the following way: For any non-negative integer x, h(x) equals f(g(x)). Assume that decompose1 will be called only on arguments for which such a function g exists. Furthermore, assume that there is no recursion depth limit in Python. decompose1 (f, h): Return g such that h(x) equals f(g(x)) for any non - negative integer x. >>> add_ one = lambda x: x + 1 >>> square_ then_ add_ one = lambda x: x * x + 1 >>> g = decompose1 ( add_one, square_ then_ add_ one ) >>> g (5) 25 >>> g (10) 100 g(x): r(y): if : else : r (0) (b) (2 pt) Write a number in the blank so that the final expression below evaluates to 2015. Assume decompose1 is implemented correctly. The make_adder and compose1 functions appear on the left column of page 2 of your study guide. e, square = make_adder (1), lambda x: x*x decompose1 (e, compose1 ( square, e ))(3) +