Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

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

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

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

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

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

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

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

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

Structure and Interpretation of Computer Programs Spring 2014 Test 2

RECURSION AND LINKED LISTS 3

In addition to the correct answer, you MUST show all your work in order to receive full credit.

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

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

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

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

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

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

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

Structure and Interpretation of Computer Programs

CS3 Midterm 2 Summer 2008

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

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

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

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

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS61A Midterm 2 Review (v1.1)

ENVIRONMENT DIAGRAMS AND RECURSION 2

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Midterm solutions. n f 3 (n) = 3

Structure and Interpretation of Computer Programs Spring 2016 Test 1

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

TREES AND ORDERS OF GROWTH 7

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

University of Massachusetts Lowell

Prelim One Solution. CS211 Fall Name. NetID

Structure and Interpretation of Computer Programs

LINKED LISTS AND MIDTERM REVIEW

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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


Structure and Interpretation of Computer Programs

CS 111X - Spring Final Exam - KEY

YOUR NAME PLEASE: *** SOLUTIONS ***

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

CSE341 Spring 2017, Final Examination June 8, 2017

Midterm 2 Review Fall 2017 October 13, Lists & Tree Recursion. Instructions

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

DRAWING ENVIRONMENT DIAGRAMS

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Largest Online Community of VU Students

CSE341 Spring 2017, Final Examination June 8, 2017

CS 1301 Exam 1 Fall 2014

20 Some terminology regarding trees: Parent node: A node that has branches. Parent nodes can have multiple branches.

CSE413 Midterm. Question Max Points Total 100

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CS103 Handout 14 Winter February 8, 2013 Problem Set 5

CS 1301 Exam 1 Fall 2014

To earn the extra credit, one of the following has to hold true. Please circle and sign.

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CS 1301 Exam 1 Spring 2011

CS8 Final Exam E03, 09M, Phill Conrad, UC Santa Barbara 09/10/2009

Scheme Basics > (butfirst '(help!)) ()

Second Midterm Exam March 21, 2017 CS162 Operating Systems

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

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Transcription:

CS 61A Structure and Interpretation of Computer Programs Summer 2014 Midterm 1 INSTRUCTIONS You have 2 hours to complete the exam. The exam is closed book, closed notes, and closed electronics, except one hand-written 8.5 11 cheat sheet of your own creation, and The Environment Diagram Rules. Mark your answers ON THE EXAM ITSELF. Answers outside of the space allotted to problems will not be graded. If you are not sure of your answer you may wish to provide a brief explanation. Full 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) 1. (1 points) Your thoughts? What makes you happy? (Alternatively, draw us a nice doodle). You can also take this opportunity to give us feedback.

2 2. (8 points) What will Python output? Include all lines that the interpreter would display. If it would display a function, then write Function. If it would cause an error, write Error. Assume that you have started Python 3 and executed the following. These are entered into Python exactly as written. welcome (): if a == 0: hello, welcome to your exam prepare for tricks. last_night (n): for i in range (n): exams pi = [3, 1, 4, 1, 5, 9, 2, 6, 5, 4] cut = lambda thing : thing [ 2:] slice_ of = lambda thing : thing [ 2: 8: 2] mystery (x): if x and (x + 1): mystery mystery Expression Interactive Output 4 4 print(5) 5 welcome() last_night(308) (lambda x, y: x + y(x))(4, lambda y: 5) [3 for x in range(30) if x > 26] cut(slice_of(pi)) cut(mystery(-1)(20)) cut(mystery(20)(-1)) print(mystery(print(20)))

Login: 3 3. (12 points) Environment Diagrams (a) (6 pt) Environmental, my dear Watson 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. You may want to keep track of the stack on the left, but this is not required. 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 value for each local frame. The first function created by lambda should be labelled λ 1, the next one should be λ 2, and so on. watson(friend): trick = 5 holmes(): friend 10 holmes adler(detective): trick = 4 detective(trick) moriarty = adler(watson) moriarty() Global frame watson adler moriarty func watson(friend) [p=global] func adler(detective) [p=global] Stack global

4 (b) (6 pt) Well... that escalated quickly Note: This is a hard question. 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. You may want to keep track of the stack on the left, but this is not required. You should be extra careful here. Hint: What is the operator? What is the operand? 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 value for each local frame. The first function created by lambda should be labelled λ 1, the next one should be λ 2, and so on. back = 10 forth(forth): back = lambda back: forth // back(15) back going = forth(back)(lambda forth: 5) Global frame back forth 10 func forth(forth) [p=global] going Stack global

Login: 5 4. (5 points) Here We Go Again Define a function wheres waldo, which takes in a linked list which may or may not contain the string Waldo as an element, and s the index of Waldo if it exists somewhere in the list, and Nowhere if it does not. Do not assume we have get item ined. Note that linked list is not a deep linked list. wheres_ waldo ( linked_ list ): >>> lst = link (" Moe ", link (" Larry ", link (" Waldo ", link (" Curly ", empty )))) >>> wheres_waldo ( lst ) 2 >>> wheres_waldo ( link (1, link (2, empty ))) Nowhere if : elif : found_ him = wheres_ waldo ( rest ( linked_ list )) if : 5. (12 points) Piled Higher and Deeper (a) (4 pt) Higher List Magic Write the function inhexing, which takes in a Python list of numbers lst, a function hex, and an integer n, and s a new list where every n th element is replaced by the result of calling hex on that element. inhexing (lst, hex, n): >>> inhexing ([1, 2, 3, 4, 5], lambda x: Poof!, 2) [1, Poof!, 3, Poof!, 5] >>> inhexing ([2, 3, 4, 5, 6, 7, 8], lambda x: x + 10, 3) [2, 3, 14, 5, 6, 17, 8] result = for i in range ( len ( lst )): if : else : result

6 (b) (8 pt) Deeper List Magic Now write deep inhexing, for deep Python lists. It takes in a DEEP Python list, a function, and a number. It s a new list where every n th element is replaced by the function applied to that element. If it encounters a list as an element, it recurses on the sublist, resetting the counter, even if the sublist was an nth element. Recall you can use the expression type(x) == type([]) to test if x is a Python list. Make sure you read and understand all the doctests! deep_inhexing (lst, hex, n): >>> deep_inhexing ([1, 2, 3, 4, 5, 6], lambda x: x + 10, 3) [1, 2, 13, 4, 5, 16] >>> deep_inhexing ([1, [[2]], [3, 4, [5]]], lambda x: Poof!, 1) [ Poof!, [[ Poof! ]], [ Poof!, Poof!, [ Poof! ]]] >>> deep_inhexing ([1, [2], 3], lambda x: Poof!, 2) [1, [2], 3] >>> deep_inhexing ([1, [2, 3], 4, [5, 6]], lambda x: Poof!, 2) [1, [2, Poof! ], 4, [5, Poof! ]] >>> deep_inhexing ([[2, 3], 4, [5, 6], [7]], lambda x: Poof!, 2) [[2, Poof! ], Poof!, [5, Poof! ], [7]] >>> deep_inhexing ([2, [4, [6, [8, 10]]]], lambda x: Poof!, 2) [2, [4, [6, [8, Poof! ]]]] helper ( lst, counter ): if : first, rest = lst [0], lst [1:] if : elif counter % n == 0: else : helper (, ) 6. (2 points) Data Abstraction True or False: Code that uses ADTs may behave as normal when you commit a Data Abstraction Violation. If True, explain why we care about ADTs. If False, explain what would break. The statement is (write True/False): Explanation:

Login: 7 7. (5 points) Recursion on Tree ADT Define a function dejavu, which takes in a tree of numbers t and a number n. It s True if there is a path from the root to a leaf such that the sum of the numbers along that path is n and False otherwise. Reminder: The constructor and selectors are tree, datum and children. dejavu (t, n): >>> my_tree = tree (2, [ tree (3, [ tree (5), tree (7)]), tree (4)]) >>> dejavu ( my_tree, 12) # 2 - > 3 - > 7 True >>> dejavu ( my_tree, 5) # Sums of partial paths like 2 - > 3 don t count False if children (t) == []: for : if : False 8. (3 points) Orders of Growth (a) (1 pt) Consider the following function inition: foo (n): times_ table = [ n * i for i in range (1, 11) ] for num in times_ table : print ( num ) What is the order of growth for a call to foo(n)? (b) (1 pt) Now consider the following function inition: bar (n): if n == 3: three! for i in range (n // 2): bar (3) What is the order of growth for a call to bar(n)? (c) (1 pt) Now consier the following function inition: spam (n): for i in range (n): for j in range (i): spam ( n - 1) What is the order of growth for a call to spam(n)?

8 9. (2 points) Newton s Method Show how you would use Newton s method to find the golden ratio φ. The golden ratio is ined as the positive solution to φ 2 = φ + 1 Here are the functions available to you, as ined in lecture: find_ zero ( f, df, x =1) # Finds the zero of the function f. deriv ( f) # Returns a function that computes f ( x) easy_ find_ zero ( f, x =1) # Finds the zero of the function f. ( ) 10. (3 points) (Extra Credit) Halting Problem (a) (1 pt) Describe the domain and range of will halt and also what will halt does. (b) (2 pt) Consider the function will number. It takes as input a function f and an input x to that function. It s True if f(x) would evaluate to a number, and False otherwise. Note in particular that even if f(x) would cause an error or an infinite loop, will number would still False. We will use the idea that will halt does not exist to prove that will number does not exist. Fill in the blanks in the proof below: Assume for contradiction Then we can construct will halts as follows: will_halt (f, x): g(y): f(y) 8 will number (g, x) But we know that So, will number cannot exist.