Structure and Interpretation of Computer Programs

Similar documents
Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

LINKED LISTS AND MIDTERM REVIEW

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

LINKED LISTS AND MIDTERM REVIEW 6

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Solutions to CS 61A Challenge Problems: Midpoint Review

Structure and Interpretation of Computer Programs

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

TREES AND ORDERS OF GROWTH 7

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Spring 2017 CS 1110/1111 Exam 1

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

CSE 247 Data Structures and Algorithms Fall Exam I

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 7

Structure and Interpretation of Computer Programs

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

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

OOP, Nonlocal, Trees, LLs, Growth Spring 2019 Guerrilla Section 3: March 16, 2019 Solutions 1 OOP. Questions

Tree-Structured Data. A tree can contains other trees: [5, [6, 7], 8, [[9], 10]] (+ 5 (- 6 7) 8 (* (- 9) 10))

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

STREAMS, ITERATORS, AND BINARY TREES 10

CSE Winter 2015 Quiz 1 Solutions

Midterm solutions. n f 3 (n) = 3

Structure and Interpretation of Computer Programs

Introduction to Spring 2007 Artificial Intelligence Midterm Solutions

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Quiz 1 Practice Problems

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

CS 111X - Fall Test 1

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

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

Structure and Interpretation of Computer Programs

Final Examination CSE 100 UCSD (Practice)

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Part 1: Multiple Choice Circle only ONE answer. Each multiple choice question is worth 3.5 marks.

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

UNIVERSITY REGULATIONS

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

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

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

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

Trees. CSE 373 Data Structures

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

Structure and Interpretation of Computer Programs

CS 2316 Exam 4 Fall 2011

Structure and Interpretation of Computer Programs

Technical University of Denmark

CSM Mock Final Spring 2018

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

Data Structures CSCI C343, Fall 2014

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Spring 2017 CS 1110/1111 Exam 2

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

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

CS 1301 Exam 1 Spring 2014

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

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

CSE 131 Introduction to Computer Science Fall Exam II

TREES AND ORDERS OF GROWTH 8

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

Quiz 1 Practice Problems

Structure and Interpretation of Computer Programs

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

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

(the bubble footer is automatically inserted into this space)

CS 1301 Exam 2 A Fall 2015

CS 2316 Exam 1 Spring 2014

Data Structures Question Bank Multiple Choice

Final Exam in Algorithms and Data Structures 1 (1DL210)

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

EECS Sample Midterm Exam

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Transcription:

CS 61A Spring 2017 Structure and Interpretation of Computer Programs Test 2 (revised) Solutions INSTRUCTIONS You have 2 hours to complete the exam. The exam is open book, open notes, closed computer, closed calculator. 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 Room in which you are taking exam Seat number in the exam room I pledge my honor that during this examination I have neither given nor received assistance. (please sign)

2 Reference Material. # Linked Lists class Link: """A linked list cell. >>> L = Link(0, Link(1)) >>> L.first 0 >>> L.rest Link(1) >>> L.first = 2 >>> L Link(2, Link(1)) >>> L.rest = Link.empty >>> L Link(2) """ empty = () # Trees def init (self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest def repr (self): if self.rest is Link.empty: return "Link({})".format(self.first) else: return "Link({}, {})".format(self.first, self.rest) class Tree: """A tree node.""" def init (self, label, branches=[]): for c in branches: assert isinstance(c, Tree) self.label = label self.branches = branches def is_leaf(self): return not self.branches

Name: 3 1. (12 points) Pointers In the following problems, single boxes are variables that contain pointers, and double boxes are Links (see the definition of Link on page 2). To show that a box contains a pointer to the empty list, draw the box like this: In parts (a) and (b), add arrows and values to the object skeletons on the right to show the final state of the program. Not all boxes will be used. (For examples of what kinds of box and pointer diagrams we re looking for, you might look at parts (c) and (d) first.) (a) (3 pt) listy = Link(0, Link(1)) def nest(l): if L is Link.empty: return L N = nest(l.rest) L.first = Link(L.first, N) return L.first listy: linky: 0 1 linky = nest(listy) (b) (3 pt) v = Link(0, Link(1, Link(2))) e = v.rest.rest e.rest = v.rest v.rest.rest = v v.rest = Link.empty e: v: 0 1 2

4 (c) (3 pt) Show Python code that will produce the situation shown in the diagram. (An arrow pointing to a Link may be shown as pointing anywhere on the double box for that Link.) v: 0 1 v = Link(None, Link(0, Link(1))) v.first = v.rest v.rest.rest.rest = v (d) (3 pt) Show Python code that converts the situation shown above the line into that shown below the line. Assume n is even. v: 1 2 3 4... n-1 n w:... v: 1 2 3 4... n-1 n def split2(l): """Assuming that linked list L has even length, breaks L into two-element linked lists, and returns a linked list of those lists.""" if L is Link.empty: else: return Link. empty result = Link(L, split2(l.rest.rest)) L.rest.rest = Link.empty return result w = split2(v)

Name: 5 2. (6 points) Complexity (a) (1.5 pt) Indicate which of the following assertions are true by circling the letters for those statements. An assertionsuch as Θ(f(n)) Θ(g(n)) means anyfunction that is in Θ(f(n)) is also in Θ(g(n)), and Θ(f(n)) = Θ(g(n)) if and only if Θ(f(n)) Θ(g(n)) and Θ(g(n)) Θ(f(n)). A. If f(n) Θ(1) and g(n) Θ(1), then Θ( f(n) + g(n) ) Θ(1). B. If Θ(f(n)) = Θ(g(n)), and g(n) > 0 everywhere, then f(n)/g(n) Θ(1). C. Θ(x 2 ) Θ(x 3 ). D. Θ(2 x ) Θ(2 x +x 2 ). E. If f(n) Θ(1000 x 3 ), then f(20) > 800. (b) (1.5 pt) Consider the function def num_kinks(l): c = 0 i = 0 while i < len(l): j = i while j < len(l): while j < len(l): if kink(l[i], L[j]): c += 1 break j += 1 j += 1 i += 1 return c Circle the order of growth that best describes the worst-case execution time (measured by the number of calls to kink) of a call to num_kinks as a function of N, the length of L. A. Θ(logN) B. Θ(N) C. Θ(N 2 ) D. Θ(N 3 ) E. Θ(2 N )

6 (c) (1.5 pt) Consider the following function on Trees def count_subtrees(t, p): if p(t.label): return 1 else: return sum([count_subtrees(child, p) for child in T.branches]) Assuming that the maximum number of children of any node is 3, circle the order of growth that best describes the worst-caseexecution time (measured by the number of calls to p) of a call to count_subtreesas a function of N, the number of nodes in T. A. Θ(logN) B. Θ(N) C. Θ(N 2 ) D. Θ(N 3 ) E. Θ(3 N ) (d) (1.5 pt) For the same function as in part (c) above, and again assuming that the maximum number of children of any node is 3, circle the order of growth that best describes the worst-case execution time (measured by the number of calls to p) of a call to count_subtrees as a function of H, the height of T. A. Θ(logH) B. Θ(H) C. Θ(H 2 ) D. Θ(H 3 ) E. Θ(3 H ). 3. (1 points) From the Sum of Human Knowledge This Renaissance composer, famous for his harmonically innovative madrigals, was also infamous for murdering his wife and her lover and thereafter having himself beaten regularly by one of his servants. Who was he? Carlo Gesualdo

Name: 7 4. (8 points) OOPs! 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. No answer requires more than 3 lines. (It s possible that all of them require even fewer.) class Thing: id = 0 def fidget(self, n): print(n, "A", self.id) def fuss(self, x, n): print(n, "B") self.fidget(n) x.fidget(n) def twitch(self, n): self.waffle(n) class Gadget(Thing): id = 1 class Whatsit(Gadget): def fidget(self, n): print(n, "D", self.id) def waffle(self, n): print(n, "D") def fiddle(self, x, n): x.waffle(n) t1 = Thing() t2 = Gadget() t3 = Whatsit() t4 = Whatsit() t3.id = 2 Expression t3.fidget(1) t4.fidget(2) t4.fuss(t1, 3) t4.fiddle(t4, 4) t4.fiddle(t1, 5) Thing.id = 3 t1.fidget(6) Interactive Output 1 D 2 2 D 1 3 B 3 D 1 3 A 0 4 D ERROR 6 A 3

8 5. (8 points) Inflections Fill in the definition of class Wrinkles to conform to its doc comment. You need not use all the lines shown. class Wrinkles: """An object that contains a sequence of items and a predicate (true/false function) and that, when iterated over, produces adjacent pairs of items in the sequence that satisfy the predicate. >>> w = Wrinkles([1, 2, 3, 2, 4, 8, 5, 4], lambda x, y: x > y) >>> for p in w:... print(p) (3, 2) (8, 5) (5, 4) """ def init (self, L, wrinkle): self._l = L self._wrinkle = wrinkle def iter (self): for k in range(1, len(self._l)): if self._wrinkle(self._l[k - 1], self._[k]): yield (self._l[k - 1], self._[k])

Name: 9 6. (8 points) Tree Paths Given a tree, t, find the length of the longest downward sequence of node labels in the tree that are increasing consecutive integers. For example, in this tree, the longest such sequence has three labels (1, 2, 3): 1 2 1 2 3 As illustrated, the longest sequence can start and end anywhere in the tree, not just the root. (Hint: don t forget there s a max function.) [The original skeleton was flawed. [The solution below shows what we were expecting, but does not properly take care of all cases where the longest path does not start at the root of the tree. The next page shows a complete solution, based on a different solution.] def longest_seq(t): """The length of the longest downward sequence of nodes in T whose labels are consecutive integers. >>> t = Tree(1, [Tree(2), Tree(1, [Tree(2, [Tree(3, [Tree(0)])])])]) >>> longest_seq(t) # 1 -> 2 -> 3 3 >>> t = Tree(1) >>> longest_seq(t) 1 """ if t.is_leaf(): return 1 max_len = 1 for b in t.branches: if b.label == t.label + 1: else: return max_len max_len = max(max_len, 1 + longest_seq(b)) max_len = max(max_len, longest_seq(b)) 0

10 Here is a proper solution, based on the corrected skeleton def longest_seq(tr): """The length of the longest downward sequence of nodes in TR whose labels are consecutive integers. >>> t = Tree(1, [Tree(2), Tree(1, [Tree(2, [Tree(3, [Tree(0)])])])]) >>> longest_seq(t) # 1 -> 2 -> 3 3 >>> t = Tree(1) >>> longest_seq(t) 1 """ max_len = 1 def longest(t): """Returns longest downward sequence of nodes starting at T whose labels are consecutive integers. Updates max_len to that length, if greater.""" nonlocal max_len n = 1 if not t.is_leaf(): for b in t.branches: L = longest(b) if b.label == t.label + 1: n = max(n, L + 1) max_len = max(n, max_len) return n longest(tr) return max_len

Name: 11 This page deliberately blank.

12 7. (8 points) Grafting We want to insert ( graft ) branches from a sequence of trees onto a tree in places where a non-leaf node has fewer than K branches, where K is a parameter. For example, given the list of four trees G created by G = [ Tree(2, [Tree(0), Tree(1)]), Tree(3), Tree(4), Tree(5) ] and the tree T1 shown below, we want T2 = graft(t1, G, 3) to destructively (and without creating any new tree nodes) turn T1 into the tree T2: G: 2 3 4 5 0 1 T1: A B C D E F G H J T2: A B C 5 D E 2 F G H 0 1 J 3 4 The list of trees (G in the example above) will always have enough items to fill all necessary places. Trees are inserted in postorder (that is, bottom to top, left to right).

Name: 13 Fill in the graft function here. You need not use all the lines. def graft(t, L, k): """Returns the Tree created by destructively adding trees from L to non-leaf nodes of Tree T with fewer than K branches. Assume that L has enough items to fill all necessary places. Fill in trees in postorder (bottom to top, left to right).""" grafts = iter(l) def do_grafts(tr): if not tr.is_leaf(): for b in tr.branches: do_grafts(b) while len(tr.branches) < k: tr.branches.append(next(grafts)) do_grafts(t) return T