Structure and Interpretation of Computer Programs

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

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 2019 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 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 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 Summer 2015 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

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 Spring 2014 Test 2

ENVIRONMENT DIAGRAMS AND RECURSION 2

CS3 Midterm 2 Summer 2008

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Introduction to Fall 2014 Artificial Intelligence Midterm Solutions

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

LINKED LISTS AND MIDTERM REVIEW

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Structure and Interpretation of Computer Programs

COS 226 Midterm Exam, Spring 2009

CS 1110: Introduction to Computing Using Python Loop Invariants

CS3 Midterm 2 Standards and Solutions

CS 111X - Fall Test 1

CS 234 Python Review Part 2

CS Lecture 19: Loop invariants

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

Midterm I. Introduction to Artificial Intelligence. CS 188 Fall You have approximately 3 hours.

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

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

CS Prelim 2 Review Fall 2018

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Multiple-choice (35 pt.)

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

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

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

CS 2316 Exam 4 Fall 2011

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

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

Midterm I. Introduction to Artificial Intelligence. CS 188 Fall You have approximately 3 hours.

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

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

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

CSM Mock Final Spring 2018

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

Microsoft Excel Level 2

CS 2316 Exam 4 Fall 2012

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS110 Introduction to Computing Fall 2006 Midterm Exam

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

Final Exam. Introduction to Artificial Intelligence. CS 188 Spring 2010 INSTRUCTIONS. You have 3 hours.

CSI33 Data Structures

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

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

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

CS 1301 Exam 3 Spring 2014

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

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

15-110: Principles of Computing, Spring 2018

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Transcription:

CS 6A 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 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 Email (...@berkeley.edu) Login (e.g., cs6a-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. Q. 4 Total / / /4 / /50

. ( points) Mutater-tot 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. Expressions are evaluated in order, and expressions may affect later expressions. Whenever the interpreter would report an error, write Error. If execution would take forever, write Forever. Assume that you have started Python and executed the following statements: ready ( betty ): print ( len ( betty )) betty [0]. append ( betty ) return betty [ 0: ] get_set (s): ready (s) return s. pop () go(on, up ): if up: return go(on [0], up -) else : return on f = [, []] g = [[, 4], [5], 6] h = [g, g] Expression Interactive Output f.pop() [] h[].pop() 6 g[g[][0]-g[0][]] [5] len(ready(g)) g[0][][0][] 4 ready(get_set(h))[0][0] [len(go(h, k)) for k in range()] [,, 4]

Login:. ( points) Vulcans (a) (8 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 return value for each local frame. Remember: Do not add a new frame when calling a built-in function (such as abs). The built-in abs function is always written as func abs(...) [parent=global]. 4 5 6 7 8 9 0 (long): prosper(spock, ): nonlocal long if len(long) == : return spock+ long[] = (long[0]) long = long[:] prosper(long[0], abs) return spock[0]+ prosper(long, lambda trek: trek-) ([, 4]) Global frame f: [parent= ] func (long) [parent=global] Global frame f: func (long) [parent= ] [parent=global] Global f: [parent= ] long - prosper None f: prosper [parent= f ] func prosper(spock, ) [parent=f] f: [parent= ] - spock f4: func lambda(trek) [parent= ] [parent=f] lambda f: [parent= f ] trek - prosper f4: [parent= f ] spock - - func abs(...) [parent=global]

4 (b) (4 pt) Fill in the environment diagram that results from executing the code below after the entire program is finished. No errors occur during the execution of this example. A complete answer will: Add all missing values created or referenced during execution. Show the return value for each local frame. 4 5 6 7 8 scramble(egg): return [egg, over(egg)]! over(easy): easy[] = [[easy], ] return list(easy[]) egg = scramble([, 4]) Global frame scramble func scramble(egg) [parent=global] over egg func over(easy) [parent=global] f: scramble [parent=global] egg f: over [parent=global] easy

Login: 5. (4 points) Will Code for Points (a) ( pt) Implement objectify, which takes a tree data abstraction and returns an equivalent Tree instance. Both the Tree class and the tree data abstraction appear on the midterm study guide. Warning: Do not violate the tree data abstraction! (Exams are flammable.) objectify (t): Return a Tree instance equivalent to a tree represented as a list. >>> m = tree () >>> m [] >>> objectify (m) Tree () >>> r = tree (, [ tree (4, [ tree (5), tree (6)]), tree (7, [ tree (8)])]) >>> r [, [4, [5], [6]], [7, [8]]] >>> objectify (r) Tree (, [ Tree (4, [ Tree (5), Tree (6)]), Tree (7, [ Tree (8)])]) return Tree ( root (t), [ objectify (b) for b in branches (t )]) (b) ( pt) Circle the Θ expression that describes the number of Tree instances constructed by calling objectify on a tree with n nodes. Θ() Θ(log n) Θ(n) Θ(n ) Θ( n ) (c) (4 pt) Implement closest, which takes a Tree of numbers t and returns the smallest absolute difference anywhere in the tree between an entry and the sum of the entries of its branches. The Tree class appears on the midterm study guide. The built-in min function takes a sequence and returns its minimum value. Reminder: A branch of a branch of a tree t is not considered to be a branch of t. closest (t): Return the smallest difference between an entry and the sum of the root entries of its branches. >>> t = Tree (8, [ Tree (4), Tree ()]) >>> closest (t) # 8 - (4 + ) = >>> closest ( Tree (5, [t ])) # Same minimum as t >>> closest ( Tree (0, [ Tree (), t ])) # 0 - ( + 8) = 0 0 >>> closest ( Tree ( )) # - 0 = >>> closest ( Tree (8, [ Tree (, [ Tree (, [ Tree (5)])])])) # - = >>> sum ([]) 0 diff = abs (t. entry - sum ([b. entry for b in t. branches ])) return min ([ diff ] + [ closest (b) for b in t. branches ])

6 (d) (6 pt) Implement double_up, which mutates a linked list by inserting elements so that each element is adjacent to an equal element. The double_up function inserts as few elements as possible and returns the number of insertions. The Link class appears on the midterm study guide. double_up (s): Mutate s by inserting elements so that each element is next to an equal. >>> s = Link (, Link (4)) >>> double_ up ( s) # Inserts and 4 >>> s Link (, Link (, Link (4, Link (4)))) >>> t = Link (, Link (4, Link (4, Link (5)))) >>> double_ up ( t) # Inserts and 5 >>> t Link (, Link (, Link (4, Link (4, Link (5, Link (5)))))) >>> u = Link (, Link (4, Link ())) >>> double_ up ( u) # Inserts, 4, and >>> u Link (, Link (, Link (4, Link (4, Link (, Link ()))))) if s is Link. empty : return 0 elif s. rest is Link. empty : s. rest = Link (s. first ) return elif s. first == s. rest. first : else : return double_up (s. rest. rest ) s. rest = Link (s. first, s. rest ) return + double_up (s. rest. rest )

Login: 7 4. ( points) What color is it? (a) (6 pt) Implement the look method of the Dress class. The look method returns a Dress instance s current color when the number of times that the instance s look method has ever been invoked evenly divides the total number times that the look method of any Dress instance has ever been invoked. Otherwise, the instance s color changes to the most recently returned color from any call to look, and None is returned. class Dress : What color is the dress? >>> blue = Dress ( blue ) >>> blue. look () blue >>> gold = Dress ( gold ) >>> gold. look () gold >>> blue. look () # does not evenly divide ; changes to gold >>> Dress ( black ). look () black >>> gold. look () # does not evenly divide 5; changes to black >>> gold. look () # evenly divides 6 black >>> Dress ( white ). look () white >>> gold. look () # 4 evenly divides 8 black >>> blue. look () # evenly divides 9 gold seen = 0 color = None init (self, color ): self. color = color self. seen = 0 look ( self ): Dress. seen += self. seen += if Dress. seen % self. seen == 0: Dress. color = self. color return self. color else : self. color = Dress. color

8 (b) (6 pt) Implement decrypt, which takes a string s and a dictionary d that contains words as values and their secret codes as keys. It returns a list of all possible ways in which s can be decoded by splitting it into secret codes and separating the corresponding words by spaces. decrypt (s, d): List all possible decoded strings of s. >>> codes = {... alan : spooky,... al : drink,... antu : your,... turing : ghosts,... tur : scary,... ing : skeletons,... ring : ovaltine... } >>> decrypt ( alanturing, codes ) [ drink your ovaltine, spooky ghosts, spooky scary skeletons ] if s == : return [] messages = [] if s in d: messages. append (d[s]) for k in range (, len (s )+): first, suffix = s[:k], s[k:] if first in d: for rest in decrypt ( suffix, d): messages. append (d[ first ] + + rest ) return messages