Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

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

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

LINKED LISTS AND MIDTERM REVIEW

Structure and Interpretation of Computer Programs

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

CS 111X - Fall Test 1

Structure and Interpretation of Computer Programs

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Midterm Exam 1 Solutions

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

CMSC 201 Fall 2016 Homework 6 Functions

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

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

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

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 1 Fall 2014

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

LINKED LISTS AND MIDTERM REVIEW 6

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

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

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

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

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

MATH ALGEBRA AND FUNCTIONS 5 Performance Objective Task Analysis Benchmarks/Assessment Students:

CS150 Sample Final. Name: Section: A / B

CSE 131 Introduction to Computer Science Fall Exam I

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

Structure and Interpretation of Computer Programs

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

CS 1301 Exam 1 Fall 2014

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

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

CS110 Introduction to Computing Fall 2006 Midterm Exam

CSE373 Fall 2013, Midterm Examination October 18, 2013

CS3 Midterm 2 Summer 2008

CSE413 Midterm. Question Max Points Total 100

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

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

C ONTROL AND H IGHER O RDER F UNCTIONS

Spring 2013 CS 122C & CS 222 Midterm Exam (and Comprehensive Exam, Part I) (Max. Points: 100)

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

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

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

Unit 10: Data Structures CS 101, Fall 2018

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

COS 116 The Computational Universe Laboratory 8: Digital Logic II

UCSD CSE 101 MIDTERM 1, Winter 2008

CS150 Sample Final Solution

CS 540: Introduction to Artificial Intelligence

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

University of Waterloo Department of Electrical and Computer Engineering ECE 457A: Cooperative and Adaptive Algorithms Midterm Examination

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

Discrete Mathematics and Probability Theory Fall 2015 Rao Midterm 1

Midterm #1 Fall minutes

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

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

Transcription:

CS 61A Structure and Interpretation of Computer Programs Fall 2015 Midterm 2 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 o cial CS 61A 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 1. (12 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.14] init (self, py): self.ok = self.py Ok.py.append(3 * py) my(self, eye): print(self.my(eye)) self.ok.pop() str (self): str(self.ok)[:4] class Go(Ok): my(self, help): [help+3, len(ok.py)] oh = Go(5) Go.py = [3, 1, 4] oh.no = { just : Go(9)} Expression z * 3 print(4, 5) + 1 Interactive Output zzz 45 Error oh.py oh.my(3) oh.ok + oh.no[ just ].ok print(oh) Ok( go ).my(5) Ok.my(oh, 5)

Name: 3 2. (14 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 value for each local frame. You are not required to write index numbers in boxes. 1 2 3 4 5 6 7 8 9 10 11 12 13 f(it): it.append(it[1]()) b(it): steps(): nonlocal it it = fit[0] fit.pop() steps fit = [1, [2]] bit = [fit, b(fit[1])] f(bit) Global f b fit bit f1: b [parent=global] func f(it) [parent=global] func b(it) [parent=global] 0 2 Return Value func steps() [parent=f1] f2: [parent= ] Return Value f3: [parent= ] 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 = [1, 1] Write numbers (1, 2, & 3) in these boxes #1 Complete these diagrams Global frame a 1 1 b = [1, 1] b c = a + [b] d = c[1:2] while a: c d 1 1 2 b.extend([[a.pop()]]) d, b = b, d a = b b[2][0], d = c, b[2] #2 Global frame a b 1 1 2 0 1 c 1 1 2 d #3 Global frame a b c d 1 1 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 s whether t is d-k-complete. Atreeisd-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 0. The built-in all function takes a sequence and s whether all elements are true values: all([1, 2]) is True but all([0, 1]) is False. Tree appears on the Midterm 2 Study Guide. complete(t, d, k): Return whether t is d-k- complete. >>> complete( Tree(1), 0, 5) True >>> u = Tree(1, [ Tree(1), Tree(1), Tree(1)]) >>> [ complete(u, 1, 3), complete(u, 1, 2), complete(u, 2, 3) ] [True, False, False] >>> complete( Tree(1, [u, u, u]), 2, 3) True if not t.branches: bs = [ ] 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 s its elements in reverse order. Assume that x[0] and y[0] are both positive. 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 = 400 [4, 0, 0] >>> adder(a, [8, 3, 4]) # 400 + 834 = 1234 [1, 2, 3, 4] >>> adder(a, [3, 3, 3, 3, 3]) # 1234 + 33333 = 34567 [3, 4, 5, 6, 7] carry, i = 0, len(x)-1 for d in reversed([0] + y): if : x.insert(0, 0) i = 0 d = carry + x[i] + d if x[0] == 0: x.remove(0) x

6 (c) (6 pt) Implement multiples, which takes a positive integer k and a linked s of digits greater than 0 and less than 10. It s 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. TheLink class appears on the Midterm 2 Study Guide. multiples(k, s): Return a linked of all multiples of k selected from digits in s. >>> odds = Link(1, Link(3, Link(5, Link(7, Link(9))))) >>> multiples(5, odds) Link(135, Link(15, Link(35))) >>> multiples(7, odds) Link(1379, Link(357, Link(35))) >>> multiples(9, odds) Link(1359, Link(135)) >>> multiples(2, odds) () t = Link.empty f(n, s): if s is Link.empty: if : t = else: f(0, s) t (d) (2 pt) Circle the expression that describes the length of the linked ed by multiples(1, s) for an input of length n. Assume multiples is implemented correctly. (1) (log n) (n) (n 2 ) (2 n )

Name: 7 (e) (8 pt) Implement int_set, which is a higher-order function that takes a of non-negative integers called contents. It s a function that takes a non-negative integer n and s 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 1 + 4 equals pow(2, 0) + pow(2, 2). Thebits helper function encodes a of nums using sequences of 0 s and 1 s that tell you whether each power of 2 is used, starting with pow(2, 0). Note: You may not use built-in tests of membership, such as an in expression or a s index method. bits(nums): A set of nums represented as a function that takes entry, 0, or 1. >>> t = bits([4, 5]) # Contains 4 and 5, but not 2 >>> t (0)(0)(1)( entry ) # 4 = 0 * pow(2, 0) + 0 * pow(2, 1) + 1 * pow(2, 2) True >>> t (0)(1)( entry ) # 2 = 0 * pow(2, 0) + 1 * pow(2, 1) False >>> t (1)(0)(1)( entry ) # 5 = 1 * pow(2, 0) + 0 * pow(2, 1) + 1 * pow(2, 2) True branch(last): if last == entry : 0 in nums ([ for k in nums if ]) branch int_set(contents): Return a function that represents a set of non- negative integers. >>> int_set([1, 2])(1), int_set([1, 2])(3) # 1 in [1, 2] but 3 is not (True, False) >>> s = int_set([1, 3, 4, 7, 9]) >>> [s(k) for k in range(10)] [ False, True, False, True, True, False, False, True, False, True] index = contains(n): t = index while n: last, =, t = ( ) t( entry ) contains

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