Structure and Interpretation of Computer Programs Fall 2016 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

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

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

Examination Questions Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

LINKED LISTS AND MIDTERM REVIEW

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

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

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

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

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

CS 2316 Exam 4 Fall 2011

LINKED LISTS AND MIDTERM REVIEW 6

TREES AND ORDERS OF GROWTH 7

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

Midterm #1 Fall minutes

Structure and Interpretation of Computer Programs

Midterm solutions. n f 3 (n) = 3

Analysis of Algorithms

CPS 231 Exam 2 SOLUTIONS

Computer Science Foundation Exam

TREES AND ORDERS OF GROWTH 7

IMPORTANT: Circle the last two letters of your class account:

University of Massachusetts Lowell

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Structure and Interpretation of Computer Programs

Midterm Exam 1 Solutions

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

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

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

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

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

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

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

CSE332 Summer 2010: Midterm Exam Sample Solutions

PROBLEM 1 : (And the winner is...(12 points)) Assume you are considering the implementation of a priority queue that will always give you the smallest

EXAM PREPARATION SECTION 1

Structure and Interpretation of Computer Programs

CS 540: Introduction to Artificial Intelligence

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

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

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

Technical University of Denmark

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

CS 10, Fall 2015, Professor Prasad Jayanti

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

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

(the bubble footer is automatically inserted into this space)

CSL 201 Data Structures Mid-Semester Exam minutes

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

CS 222/122C Fall 2016, Midterm Exam

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

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

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

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

DATA STRUCTURES AND ALGORITHMS

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

Transcription:

CS 61A Structure and Interpretation of Computer Programs Fall 2016 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 official 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 CalCentral email (_@berkeley.edu) TA 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)

2 1. (8 points) Halloween 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 an error occurs, write Error, but include all output displayed before the error. The Link class appears on page 2 of the midterm 2 study guide. The first two rows have been provided as examples. Recall: The interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have started python3 and executed the following statements: class Party : guests = Link. empty init (self, time ): Party. guests = Link ( time +1, Party. guests ) attend ( self ): self. guests. rest = Link ( self. guests. rest ) self. guests class Costume ( Party ): init (self, bow, tie ): Party. guests. rest = Link ( bow ) self.ie = Link ( self ) attend ( self ): print ( repr ( self.ie )) Party. attend = lambda self : Party ( 9). guests repr ( self ): print ( Nice ) Costume Expression Link(1, Link.empty) Link(1, Link(2)) Interactive Output Link(1) Link(1, Link(2)) Party(1).guests Party(3).attend() Costume(5, 6).attend() Party(7).attend()

Name: 3 2. (8 points) A List with a Twist 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 frames. Add all missing values created or referenced during execution. Show the value for each local frame. Use box-and-pointer notation for list values. You do not need to write index numbers or the word list. 1 2 3 4 5 6 7 8 9 10 11 12 lamb = 'da' da(da): lamb(lamb): nonlocal da da(nk): da = nk + ['da'] da.append(nk[0:2]) nk.pop() da(lamb) da([[1], 2]) + 3 da(lambda da: da(lamb)) Global frame f1: [parent= ] Return Value f2: [parent= ] Return Value f3: [parent= ] Return Value f4: [parent= ] Return Value

4 3. (6 points) Return Policy Implement quota, which takes a one-argument function f and a non-negative integer limit. The function it s has the same behavior as f, except that each value is only ed up to limit times. After that, the function s an Over quota message instead, and the limit is decreased by 1 for future calls. quota (f, limit ): A decorator that limits the number of times a value can be ed. >>> square = lambda x: x * x >>> square = quota ( square, 3) >>> square (6) # 1 st call with value 36 36 >>> [ square (5) for x in range ( 3)] # 3 calls when the limit is 3 [25, 25, 25] >>> square (5) # 4 th call with value 25 Over quota! Limit is now 2 >>> square ( -6) # 2 nd call with value 36 36 >>> square ( -6) # 3 rd call when the limit is 2 Over quota! Limit is now 1 >>> square (7) # 1 st call when the limit is 1 49 >>> square (5) # 5 th call with value 25 Over quota! Limit is now 0 values = [] limited (n): y = count = len ( ) values. append (y) if : limit = Over quota! Limit is now limited

Name: 5 4. (6 points) A Classy Election Implement the VotingMachine and Ballot classes based on the doctest below. The voting machine must determine which choice has the most votes (the winner) and detect if a ballot is used more than once. In case of a tie, the winner among choices with maximal votes is the one that most recently received a vote. Ballot.vote takes a string, and a VotingMachine must handle an arbitrary number of choices. class VotingMachine : A machine that creates and records ballots. >>> machine = VotingMachine (4) >>> a, b, c, d = machine. ballots >>> d. vote ( Bruin ) Bruin is winning >>> b. vote ( Bruin ) Bruin is winning >>> c. vote ( Bear ) Bear is losing >>> a. vote ( Bear ) Bear is winning >>> c. vote ( Tree ) Fraud : multiple votes from the same ballot! >>> machine. winner Bear init (self, k): self. ballots = [ for i in range (k)] self. votes = {} record ( self, ballot, choice ): if ballot. used : Fraud : multiple votes from the same ballot! self. votes [ choice ] = + 1 if : choice + is losing else : choice + is winning class Ballot : init (self, machine ): self. machine = machine vote (self, x):

6 5. (6 points) Trick or Tree Implement path, which takes a linked list s and a Tree instance t. It s whether s is a path from the root of t to some leaf. The Tree and Link classes are on page 2 of the midterm 2 study guide. Restrictions: You may not call the built-in len function on a linked list or invoke its len method. You may not apply element selection (e.g., s[2]) on a linked list or invoke its getitem method. path (s, t): Return whether Link S is a path from the root to a leaf in Tree T. >>> t = Tree (1, [ Tree (2), Tree (3, [ Tree (4), Tree (5)]), Tree (6)]) >>> a = Link (1, Link (3, Link (4))) # A full path >>> path (a, t) True >>> b = Link (1, Link ( 3)) # A partial path >>> path (b, t) False >>> c = Link (1, Link (2, Link ( 7))) # A path and an extra value >>> path (c, t) False >>> d = Link (3, Link ( 4)) # A path of a branch >>> path (d, t) False if : False if : True ([ for b in t. branches ])

Name: 7 6. (6 points) Left it Right There (a) (4 pt) Implement binary, which takes a list s of unique integers. It s a binary search tree containing all of those integers, represented as a BTree instance or BTree.empty. The values in any path of this tree must appear in the same order as they did in s. The BTree class is on page 2 of the midterm 2 study guide. binary (s): Construct a binary search tree from S for which all paths are in order. >>> binary ([3, 5, 1]) BTree (3, BTree (1), BTree (5)) >>> binary ([4, 3, 7, 6, 2, 9, 8]) BTree (4, BTree (3, BTree (2)), BTree (7, BTree (6), BTree (9, BTree (8)))) assert len ( s) == len ( set ( s)), All elements of s should be unique if : root = left = right = BTree (root, binary ( left ), binary ( right )) (b) (1 pt) Circle the Θ expression that describes the smallest possible height of the tree ed by binary(s) for a list s of length n. The height of a tree is the length of the longest path from its root to a leaf. Θ(1) Θ(log n) Θ(n) Θ(n 2 ) Θ(2 n ) None of these (c) (1 pt) Circle the Θ expression that describes the largest possible height of the tree ed by binary(s) for a list s of length n. The height of a tree is the length of the longest path from its root to a leaf. Θ(1) Θ(log n) Θ(n) Θ(n 2 ) Θ(2 n ) None of these

8 7. (10 points) Summer Camp (a) (6 pt) Implement sums, which takes two positive integers n and k. It s a list of lists containing all the ways that a list of k positive integers can sum to n. Results can appear in any order. sums (n, k): Return the ways in which K positive integers can sum to N. >>> sums (2, 2) [[1, 1]] >>> sums (2, 3) [] >>> sums (4, 2) [[3, 1], [2, 2], [1, 3]] >>> sums (5, 3) [[3, 1, 1], [2, 2, 1], [1, 3, 1], [2, 1, 2], [1, 2, 2], [1, 1, 3]] if : y = [] for x in : y. extend ([ for s in sums ( )]) y (b) (4 pt) Why so many lines? Implement f and g for this alternative version of the sums function. f = lambda x, y: (x and [ for z in y] + f(, )) or [] sums (n, k): Return the ways in which K positive integers can sum to N. g = lambda w: (w and f( )) or [[]] [v for v in g(k) if sum (v) == n]