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

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

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 Summer 2014 Midterm 2

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

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

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

Structure and Interpretation of Computer Programs

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

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

Introduction to Fall 2008 Artificial Intelligence Midterm Exam

CPSC 217 Midterm (Python 3 version)

EXAM PREPARATION SECTION 1

Structure and Interpretation of Computer Programs

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2016 Test 1

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

Structure and Interpretation of Computer Programs Spring 2014 Test 2

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

: Intro Programming for Scientists and Engineers Final Exam

Structure and Interpretation of Computer Programs

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Spring 2014

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 Fall 2012 Alternate Midterm 1

COS 126 Midterm 1 Written Exam Fall 2012

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Midterm #1 Fall minutes

Structure and Interpretation of Computer Programs

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

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

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

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

CS 1301 Exam 2 Fall 2010

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

CS150 Sample Final Solution

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

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CS150 Sample Final. Name: Section: A / B

CS 115 Exam 2 (Section 1) Fall 2017 Thu. 11/09/2017

University of California, Berkeley College of Engineering

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

CS Name : Grading TA:

Practice midterm exam

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

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

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

CS 2316 Exam 1 Spring 2014

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

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Introduction to Spring 2007 Artificial Intelligence Midterm Solutions

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

Midterm Exam #2 Solutions October 25, 2016 CS162 Operating Systems

Structure and Interpretation of Computer Programs

Final Exam Version A

Structure and Interpretation of Computer Programs

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

CS 1301 Exam 2 A Fall 2015

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

Transcription:

CS 61A Fall 2012 Structure and Interpretation of Computer Programs Midterm 2 Solutions 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 two official 61A midterm study guides 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. 1 Q. 2 Q. 3 Q. 4 Total /16 /12 /14 /8 /50

2 THIS PAGE INTENTIONALLY LEFT BLANK

Login: 3 1. (16 points) Expressionism (a) (8 pt) For each of the following expressions, write the repr string of the value to which the expression evaluates. Special cases: If an expression evaluates to a function, write Function. If evaluation would never complete, write Forever. None of these expressions cause an error. Assume that the expressions are evaluated in order. Evaluating the first may affect the value of the second, etc. Assume that you have started Python 3 and executed the following statements: def countdown (s, t): buzz = [t] def nas (a): nonlocal t t = buzz [0]+ s buzz. append (t) return s(a) def aldrin (): return buzz return nas, aldrin def endeavor (k): return k* len ( discovery ()) atlantis, discovery = countdown ( endeavor, u ) Expression Evaluates to 5*5 25 [ u ] discovery() atlantis(1) 2 atlantis(len(discovery())) 6 discovery() [ u, us, us ]

4 (b) (8 pt) For each of the following expressions, write the repr string of the value to which the expression evaluates. Special cases: If an expression evaluates to a function, write Function. If evaluation would never complete, write Forever. None of these expressions cause an error. Assume that the expressions are evaluated in order. Evaluating the first may affect the value of the second, etc. Assume that you have started Python 3 and executed the following statements: class Lawyer ( object ): def init (self, s): if len (s) < 2: self.s = s else : self.s = Lawyer (s [2:]) def repr ( self ): return Lawyer ( + repr ( self.s) + ) def think ( self ): if hasattr (self, decide ): return self. decide () while type ( self.s) == Lawyer : self.s = self.s.s return self. s class CEO ( Lawyer ): def decide ( self ): return Denied obama = Lawyer ([ a, b, c ]) romney = CEO ([ x, y, z ]) Expression Evaluates to 5*5 25 [ c ] obama.think() obama romney Lawyer.think(romney) Lawyer([ c ]) Lawyer(Lawyer([ z ])) Denied

Login: 5 2. (12 points) Picture Frame (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. def oski(bear): def cal(): nonlocal bear if bear == 0: return bear furd = bear bear = bear - 1 return (furd, cal()) return cal() Global frame f1: oski oski bear 0 cal func oski(bear) func cal() [parent=f1] oski(2) tuple 0 1 2 tuple 0 1 1 0 cal [parent=f1] furd 2 cal [parent=f1] furd 1 cal [parent=f1] 0

6 (b) (5 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. Global frame beep func beep(oo, ee) b list beep 0 1 2 4 5 oo 0 ee 1 list 0 4 1 2 3 0 not found list 0 4 def beep(oo, ee): b[oo] = [b[ee], oo, [b[ee]]] return b[oo] b = list(range(3, 6)) beep(0, 1).append('not found') (c) (1 pt) What will print(b) output after executing this code? [[4, 0, [4], not found ], 4, 5]

Login: 7 3. (14 points) Objets d Art (a) (6 pt) Cross out whole lines in the implementation below so that the doctests for Vehicle pass. addition, cross out all lines that have no effect. Don t cross out docstrings, doctests, or decorators. class Vehicle ( object ): """ >>> c = Car ( John, CS61A ) >>> c. drive ( John ) John is driving >>> c. drive ( Jack ) Car stolen : John CS61A >>> c. pop_tire () 3 >>> c. pop_tire () 2 >>> c. fix () >>> c. pop_tire () 3 """ def init (self, owner ): self. owner = owner def move ( self ): print ( self. owner + is driving ) In class Car ( Vehicle ): tires = 4 def init (self, owner, license_plate ): Vehicle. init (self, owner ) self. plate = license_ plate self. tires = Car. tires # This line is optional def drive ( self, person ): if person!= self. owner : print ( Car stolen : + self. identification ) else : Car. move ( self ) @property def identification ( self ): return self. owner + + self. plate def pop_tire ( self ): self. tires -= 1 return self. tires def fix ( self ): setattr (self, tires, type ( self ). tires )

8 (b) (6 pt) The max path function takes an instance of the Tree class from Study Guide 2. It is meant to return the maximal sum of internal entry values on a path from the root to a leaf of the tree. def max_path ( tree ): """ Return the sum of entries in a maximal path from the root to a leaf. >>> max_path ( Tree (3, Tree (4), Tree (-2, Tree (8), Tree (3)))) 9 >>> max_path ( Tree (9, None, Tree (1, Tree (-2, Tree (5), Tree (2)), None ))) 13 """ paths = [0] if tree. right is not None : paths. append ( max_path ( tree. right )) if tree. left is not None : paths. append ( max_path ( tree. left )) tree. entry += max ( paths ) return tree. entry Circle True or False to indicate whether each of the following statements about max path is true. i. (True/False) It returns the correct result for all doctests shown. ii. (True/False) It returns the correct result for all valid trees with integer entries. iii. (True/False) It may change (mutate) its argument value. iv. (True/False) It may run forever on a valid tree. (c) (2 pt) Define a simple mathematical function f(n) such that evaluating max path(tree) on a tree with n entries performs Θ(f(n)) function calls. f(n) = n

Login: 9 4. (8 points) Form and Function (a) (4 pt) You have been hired to work on AI at UnitedPusherElectric, the leading manufacturer of Pusher Bots. The latest model, PusherBot 5, keeps pushing people down stairs when it gets lost. Fix it! Assume that you have an abstract data type position that combines x and y coordinates (in meters). >>> pos = position (3, 4) >>> x( pos ) 3 >>> y( pos ) 4 pathfinder should return a visit function that takes a position argument. visit returns True unless: i. Its argument position is more than 6 meters from position(0, 0), or ii. Its argument position has been visited before. The implementation below is incorrect. Cross out each line (or part of a line) that must change and write a revised version next to it, so that pathfinder is correct and does not depend on the implementation of position. Assume your corrections have the same indentation as the lines they replace. You may not add or remove lines. Make as few changes as necessary. from math import sqrt def equal ( position, other ): return x( position ) == x( other ) and y( position ) == y( other ) def pathfinder (): """ Return a visit function to help with path - finding. >>> visit1, visit2 = pathfinder (), pathfinder () >>> visit1 ( position (3, 4)) True >>> visit1 ( position (5, 12)) # Too far away False >>> visit1 ( position (3, 4)) # Already visited False >>> visit2 ( position (3, 4)) True """ visited = [] # was () def visit ( pos ): if sqrt ( x( pos )*x( pos ) + y( pos )*y( pos ) ) > 6: return False for p in visited : # was visit : if equal ( p, pos ): # was p == pos : return False # was True visited. append ( pos ) return True return visit # was visited

10 (b) (4 pt) Fill in missing expressions in the implementation for list anagrams, which lists all anagrams (reorderings of the letters) of a given word. You may assume that the word has no repeated letters. Some hints about string slicing appear in the doctest. def list_anagrams (w): """ List all anagrams of word w. >>> w = ate >>> w [:0] >>> w[ len (w ):] >>> list_anagrams (w) [ ate, aet, tae, tea, eat, eta ] """ if w == : return [ ] anagrams = [] for i in range ( len (w )): subgrams = list_anagrams (w[:i] + w[i +1:]) anagrams += [ w[ i] + s for s in subgrams ] return anagrams OR if w == : return [ ] anagrams = [] for i in range ( len (w )): subgrams = list_anagrams (w [1:]) anagrams += [s[:i] + w [0] + s[i:] for s in subgrams ] return anagrams (c) (0 pt) Draw a picture of PusherBot 5.