Structure and Interpretation of Computer Programs Spring 2014 Test 2

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

RLISTS AND THE ENVIRONMENT MODEL 9

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

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

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 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 2014 Midterm 2

Final Exam Solutions. Structure and Interpretation of Computer Programs. Fall 2011 /12 /16 /12 /18 /10 /12 /80 INSTRUCTIONS

Midterm Exam 1 Solutions

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

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

CS 61A: Orders of Growth. 6 December 2016

CSC236 Week 5. Larry Zhang

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

MEMOIZATION, RECURSIVE DATA, AND SETS

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

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

CS61B, Spring 2006 Final Examination P. N. Hilfinger

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

Structure and Interpretation of Computer Programs

TUPLES AND RECURSIVE LISTS 5

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

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

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CS 202: Introduction to Computation Fall 2010: Exam #1

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

University of California, Berkeley College of Engineering

CSE341 Spring 2017, Final Examination June 8, 2017

Intro. Scheme Basics. scm> 5 5. scm>

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

Structure and Interpretation of Computer Programs

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

CSE wi: Practice Midterm

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

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

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

CSE341 Spring 2017, Final Examination June 8, 2017

15-110: Principles of Computing Sample Exam #1

Structure and Interpretation of Computer Programs

LINKED LISTS AND MIDTERM REVIEW

Total Score /1 /20 /41 /15 /23 Grader

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

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

ENVIRONMENT DIAGRAMS AND RECURSION 2

CS 1301 Exam 1 Fall 2011

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

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Structure and Interpretation of Computer Programs

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

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

: Intro Programming for Scientists and Engineers Final Exam

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

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

SCHEME AND CALCULATOR 5b

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

Midterm Exam 2B Answer key

TREES AND ORDERS OF GROWTH 7

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

ECE264 Spring 2013 Final Exam, April 30, 2013

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

CS 1301 Exam 1 Spring 2011

Homework #6 (Constraint Satisfaction, Non-Deterministic Uncertainty and Adversarial Search) Out: 2/21/11 Due: 2/29/11 (at noon)

CSCE 110: Programming I

Structure and Interpretation of Computer Programs

CISC-235* Test #2 October 15, 2018

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

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

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS 115 Exam 3, Spring 2014

Transcription:

CS 61A Structure and Interpretation of Computer Programs Spring 2014 Test 2 INSTRUCTIONS You have 2 hours to complete the exam. The exam is open book and open notes. You may not use a computer, calculator, or anything responsive. Mark your answers ON THE EXAM ITSELF. If you are not sure of your answer you may wish to provide a brief explanation. Since some students will be taking the exam later, PLEASE DO NOT DISCUSS IT before discussion sections. Last name First name SID Login 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. 1 Q. 2 Q. 3 Q. 4 Q. 5 Q. 6 Total /12 /15 /8 /11 / /4 /50

2 1. (12 points) What would Python Do? (a) (9 points) For each of the following expressions, write the value to which it evaluates. The first two rows have been provided as examples. If evaluation causes an error, write Error. If evaluation never completes, write Forever. Assume that you have started Python 3 and executed the following statements: class Doge: much = "wow" def bark(self): self.much class Puppye(Doge): very = "meow" def init (self, friend): self.friend = friend self.much = [Doge.much, friend.much] def bark(self): "puppye" + Doge.bark(self)[1] such = Doge() such.much = "interesting" fido = Puppye(such) flora = Puppye(fido) Expression Evaluates to abs(-3) 3 1/0 Error Doge.much Puppye.much such.very such.bark()[0:2] fido.bark() fido.friend.much fido.friend.bark() flora.friend.bark() flora.friend.friend.much

Login: 3 (b) (3 points) Consider the following Python function: def foo(a, b): map(lambda x: a[x//2] if x % 2 == 0 else b[x//2], range(len(a+b))) What is the value of tuple(foo( (10, 5, 25), (8, 7, 11) ))? Ans:

4 2. (15 points) Assorted Diagrams (a) (8 points) Fill in the environment diagram that results from executing the following segment 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, labels, and parent annotations to all local frames. Add all missing values created during execution. Show the value for each local frame resulting from a call that has completed when execution stops (leave other values blank). a = 0 def april(fools): a = 2 def joke(a): nonlocal fools fools = lambda y: a + 4 a if fools(777) == 1: joke(a + 9) fools(april) april(lambda x: a + 1) Global Frame a april func april(fools) Return Value Return Value Return Value Return Value

Login: 5 (b) (5 points) Draw the box-and-pointer diagram that results from executing the following code: q = [1, 2] s = [1, 2, [3]] t = [4, [s, 5], 6] u = [t] We ve already drawn the diagram for q as an example. Global Frame q list 0 1 1 2 s t u (c) (2 points) Now assume the following line is executed after all of the statements above are executed. u.append(u) Show the result on your diagram. Mark any added list cells with an asterisk (*) to show what you change.

6 3. (8 points) Data Abstraction Albert loves to create salads, and decides to create functional abstractions representing vegetables and salads. (a) (2 points) Unfortunately, Albert only had time to finish writing the constructors for his abstract data types. Help him out by writing in the corresponding selector functions. def make_vegetable(name, color): """Returns a representation of a vegetable with given NAME and COLOR, which must both be strings.""" [[name], [color]] def get_name(vegetable): def get_color(vegetable): def make_salad(name, vegetables): """Return a salad having the given NAME, and constituent VEGETABLES (a list of vegetables.""" [[name], vegetables] def get_salad_name(salad): def get_vegetables(salad):

Login: 7 Condensation of the methods from part (a) for reference: def make_vegetable(name, color): [[name], [color]] def get_name(vegetable):... def get_color(vegetable):... def make_salad(name, vegetables): [[name], vegetables] def get_salad_name(salad):... def get_vegetables(salad):... (b) (5 points) Steven decides to use Albert s data abstractions to create a delicious lunch. However, he doesn t feel confident that he understands how to use Albert s abstract data types. Help him out by both: Crossing out any mistakes or data abstraction violations he has made. Writing in code that correctly conveys what he was trying to do, either on the line of code itself or below the line of code where the issue occurs. ingredients = [ make_vegetable(["lettuce"], ["green"]), [["carrot"], ["orange"]] ] first_vegetable_color = ingredients[0][1][0] lunch = make_salad( "simple_salad", ingredients + [make_vegetable("cucumber", "green")] ) salad_name = lunch[0][0] for xxxxx in lunch[1]: #Line 5. See part (c) vegetable_name = get_salad_name(xxxxx) # Line 6 vegetable_color = xxxxx[1][0] print("from " + salad_name + ", I have a " + vegetable_color + " " + vegetable_name) last_ingredient = get_vegetables(lunch)[len(get_vegetables(lunch)) - 1] print( last_ingredient.name + " was a great finishing touch!" ) (c) (1 point) What meaningful variable name could be used in place of xxxxx on lines 5 and 6?

8 4. (11 points) Programs (a) (5 points) Given a nested list of rlists, the sum of all integers weighted by their depth. In the rlist below, 1 and 2 are at depth 1, 3 is at depth 2, and 4 is at depth 3: 1 2 3 4 Here is the relevant part of the rlist implementation from lecture: empty_rlist =... def rlist(first, rest = empty_rlist):... def first(r):... def rest(r):... Hint: Python s built-in method isinstance(avalue, aclass) will tell you if avalue belongs to aclass (or a subtype of aclass). def nested_sum(rlst): """Assuming RLST to be an rlist containing only rlist or integer values, the sum of all integers in RLST, weighted by their depth. >>> simple = rlist(1, rlist(4, rlist(6))) >>> nested_sum(simple) # 1*1 + 1*4 + 1*6 11 >>> harder = rlist(1, rlist(rlist(3, rlist(rlist(4))), rlist(2))) >>> nested_sum(harder) # 1*1 + 2*3 + 3*4 + 1*2 21""" sum_at_depth(rlst, 1) def sum_at_depth(rlst, d): """The sum of all integers in RLST, weighted by their depths, assuming that RLST itself is at depth D.""" if : elif : else: # (Your answer need not use all lines.)

Login: 9 (b) (6 points) In lecture, we defined a class BinTree for describing binary trees, supporting the following operations (T denotes a BinTree value): BinTree(LABEL, LEFT, RIGHT ) A binary tree whose label is LABEL, whose left child is LEFT, and whose right child is RIGHT. BinTree.empty tree The empty tree. T.is empty True iff T is the empty tree. T.label The label of T. T.left The left child of T. T.right The right child of T. Write a function bound that takes in a binary search tree containing real numbers, and two numberslow and high. It s a new binary search tree that contains all and only the elements in the input tree that are between low and high, inclusive. So if tree is the tree on the left, bound(tree, 4, 10) will produce the tree on the right. 8 8 4 11 4 9 3 5 9 12 5 2 3.5 4.5 6 4.5 6 def bound(tree, low, high): """A binary search tree containing exactly the elements in binary search tree TREE that are between LOW and HIGH, inclusive. Assume LOW<=HIGH. if : elif : elif : else: BinTree(tree.label, )

10 5. (1 point) Sum of Human Knowledge It is a little-known fact that Santa Claus actually manages to avoid the bad arctic winter flying conditions by instead using a series of perfectly straight tunnels dug (presumably by magical means) directly through the earth from the north pole. One of them, in fact, emerges near Coffs Harbour in New South Wales at about latitude 30 S. On his bathroom scale at the north pole, Santa weighs in at 100kg (or, for those who have recently taken physics, 100kg-wt). What would the scale read if he used it while exactly halfway along this particular tunnel (±2%)? 6. (4 points) The Big Theta Consider the following function (assume that parameter S is a list): def umatches(s): result = set() for item in S: if item in result: result.remove(item) else: result.add(item) result For (b) (d) below, give the tightest bounds you can (i.e., preferably (Θ) for each): (a) (1 point) Fill in the blank: The function umatches s the set of all (b) (1 point) Let s assume that the operations of adding to, removing from, or checking containment in a set each take roughly constant time. Give an asymptotic bound on the worst-case time for umatches as a function of N = len(s).. (c) (1 point) Suppose that instead of having result be a set, we make it a list, (so that it is initialized to [] and we use.append to add an item). What now is the worst-case time bound? You can assume that.append is a constant-time operation, and.remove and the in operator require time that is Θ(L) in the worst case, where L is the length of the list operated on. Since we never add an item that is already in the list, each value appears at most once, just as for a Python set. (d) (1 point) Now suppose that we consider only cases where the number of different values in list S is at most 100, and we again use a list for result. What is the worst-case time now?