Structure and Interpretation of Computer Programs

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

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

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

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Midterm Exam 1 Solutions

Structure and Interpretation of Computer Programs

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

CS 1301 Exam 1 Fall 2014

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

Structure and Interpretation of Computer Programs

LINKED LISTS AND MIDTERM REVIEW

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

CS 111X - Fall Test 1

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

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

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 Spring 2014 Final (with corrections)

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

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

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

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

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

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

CS 1301 Exam 1 Fall 2014

CSE413 Midterm. Question Max Points Total 100

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

Final Exam Version A

CONTROL AND ENVIRONMENTS 1

Midterm #1 Fall minutes

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Examination Questions Midterm 2

Midterm solutions. n f 3 (n) = 3

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

CSI33 Data Structures

CSE373 Fall 2013, Midterm Examination October 18, 2013

C ONTROL AND H IGHER O RDER F UNCTIONS

CS 2316 Exam 4 Fall 2011

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

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

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

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

CMSC 201 Fall 2016 Homework 6 Functions

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

CS115 - Module 10 - General Trees

Structure and Interpretation of Computer Programs Spring 2014 Test 2

CS 2316 Exam 4 Fall 2011

TREES AND ORDERS OF GROWTH 7

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

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

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

Midterm I Practice Problems

CS Prelim 2 Review Fall 2018

CS150 Sample Final. Name: Section: A / B

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

CS Prelim 1 Review Fall 2018

Midterm #2a Spring minutes

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Transcription:

CS 6A Fall 25 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 crib sheet of your own creation and the official CS 6A study guides. 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 All the work on this exam is my own. (please sign)

2. (2 points) ok --submit 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 more than 3 lines are displayed, just write the first 3. If an error occurs, write Error. If evaulation would run forever, write Forever. The first two rows have been provided as examples. Assume that you have started python3 and executed the following statements (which do not cause errors): class Ok: py = [3.4] def init (self, py ): self.ok = self.py Ok.py. append (3 * py) def my(self, eye ): print ( self.my(eye )) return self.ok.pop () def str ( self ): return str ( self.ok )[:4] class Go(Ok ): def my(self, help ): return [ help +3, len (Ok.py )] oh = Go (5) Go.py = [3,, 4] oh.no = { just : Go (9)} Expression z * 3 print(4, 5) + oh.py oh.my(3) oh.ok + oh.no[ just ].ok print(oh) Ok( go ).my(5) Ok.my(oh, 5) Interactive Output zzz 4 5 Error [3,, 4] [6, 3] [3.4, 5, 27, 3,, 4] [3. Error [8, 4] gogogo

Name: 3 2. (4 points) Exercises (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 return value for each local frame. You are not required to write index numbers in boxes. 2 3 4 5 6 7 8 9 2 3 def f(it): it.append(it[]()) def b(it): def steps(): nonlocal it it = fit[] return fit.pop() return steps fit = [, [2]] bit = [fit, b(fit[])] f(bit) Global f b fit bit f: b [parent=global] it func f(it) [parent=global] func b(it) [parent=global] 2 2 steps Return Value func steps() [parent=f] f2: f [parent=global] it Return Value None f3: si [parent=f] Return Value

4 (b) (8 pt) In each box next to a line of code below, write the number of the environment diagram that would result after executing that line and complete the diagram by adding all missing values (boxes and arrows). Suggestion: You may want to write out the whole diagram elsewhere (scratch paper or the bottom left of this page) before filling in the answer area. a = [, ] Write numbers (, 2, or 3) in these boxes # Complete these diagrams Global frame a b = [, ] c = a + [b] d = c[:2] while a: b.extend([[a.pop()]]) b c d 2 d, b = b, d a = b b[2][], d = c, b[2] #2 Global frame a b c d 2 2 #3 Global frame a b c d 2 2

Name: 5 3. (24 points) Return of the Digits (a) (4 pt) Implement complete, which takes a Tree instance t and two positive integers d and k. It returns whether t is d-k-complete. A tree is d-k-complete if every node at a depth less than d has exactly k branches and every node at depth d is a leaf. Notes: The depth of a node is the number of steps from the root; the root node has depth. The built-in all function takes a sequence and returns whether all elements are true values: all([, 2]) is True but all([, ]) is False. Tree appears on the Midterm 2 Study Guide. def complete (t, d, k): Return whether t is d- k- complete. >>> complete ( Tree (),, 5) True >>> u = Tree (, [ Tree (), Tree (), Tree ()]) >>> [ complete (u,, 3), complete (u,, 2), complete (u, 2, 3) ] [True, False, False ] >>> complete ( Tree (, [u, u, u]), 2, 3) True if not t. branches : return d == bs = [ complete (b, d -, k) for b in t. branches ] return len (t. branches ) == k and all (bs) (b) (4 pt) Implement adder, which takes two s x and y of digits representing positive numbers. It mutates x to represent the result of adding x and y. Notes: The built-in reversed function takes a sequence and returns its elements in reverse order. Assume that x[] and y[] are both positive. def adder (x, y): Adds y into x for s of digits x and y representing positive numbers. >>> a = [3, 4, 5] >>> adder ( a, [5, 5]) # 345 + 55 = 4 [4,, ] >>> adder ( a, [8, 3, 4]) # 4 + 834 = 234 [, 2, 3, 4] >>> adder (a, [3, 3, 3, 3, 3]) # 234 + 33333 = 34567 [3, 4, 5, 6, 7] carry, i =, len (x)- for d in reversed ([ ] + y): if i == -: x. insert (, ) i = d = carry + x[i] + d carry, x[i], i = d //, d %, i - if x [] == : x. remove () return x

6 (c) (6 pt) Implement multiples, which takes a positive integer k and a linked s of digits greater than and less than. It returns a linked of all positive n that are multiples of k greater than k and made up of digits only from s. The digits in each n must appear in the same order as they do in s, and each digit from s can appear only once in each n. The Link class appears on the Midterm 2 Study Guide. def multiples (k, s): Return a linked of all multiples of k selected from digits in s. >>> odds = Link (, Link (3, Link (5, Link (7, Link (9))))) >>> multiples (5, odds ) Link (35, Link (5, Link (35))) >>> multiples (7, odds ) Link (379, Link (357, Link (35))) >>> multiples (9, odds ) Link (359, Link (35)) >>> multiples (2, odds ) () t = def Link. empty f(n, s): nonlocal t if s is Link. empty : if n > k and n % k == : else : t = Link (n, t) f(, s) f(n, s. rest ) f(n *+ s. first, s. rest ) return t (d) (2 pt) Circle the Θ expression that describes the length of the linked returned by multiples(, s) for an input of length n. Assume multiples is implemented correctly. Θ() Θ(log n) Θ(n) Θ(n 2 ) Θ(2 n ) Note: Θ() was also accepted if accompanied by the justification that s would not contain repeated digits.

Name: 7 (e) (8 pt) Implement int_set, which is a higher-order function that takes a of non-negative integers called contents. It returns a function that takes a non-negative integer n and returns whether n appears in contents. Your partner left you this clue: Every integer can be expressed uniquely as a sum of powers of 2. E.g., 5 equals + 4 equals pow(2, ) + pow(2, 2). The bits helper function encodes a of nums using sequences of s and s that tell you whether each power of 2 is used, starting with pow(2, ). Note: You may not use built-in tests of membership, such as an in expression or a s index method. def bits ( nums ): A set of nums represented as a function that takes entry,, or. >>> t = bits ([4, 5]) # Contains 4 and 5, but not 2 >>> t ()()()( entry ) # 4 = * pow (2, ) + * pow (2, ) + * pow (2, 2) True >>> t ()()( entry ) # 2 = * pow (2, ) + * pow (2, ) False >>> t ()()()( entry ) # 5 = * pow (2, ) + * pow (2, ) + * pow (2, 2) True def branch ( last ): if last == entry : return in nums return bits ([ k // 2 for k in nums if k % 2 == last ]) return branch def int_ set ( contents ): Return a function that represents a set of non - negative integers. >>> int_set ([, 2])(), int_set ([, 2])(3) # in [, 2] but 3 is not (True, False ) >>> s = int_set ([, 3, 4, 7, 9]) >>> [s(k) for k in range ()] [ False, True, False, True, True, False, False, True, False, True ] index = bits ( contents ) def contains (n): t = index while n: last, n = n % 2, n // 2 t = t( last ) return t( entry ) return contains

8 4. ( points) Back to the Future Draw a picture of what would happen if Fibonacci traveled through time to October 22, 25.