Structure and Interpretation of Computer Programs

Similar documents
Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

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

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

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

EXAM PREPARATION SECTION 1

MUTABLE LISTS AND DICTIONARIES 4

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

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

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

Midterm Exam 1 Solutions

CONTROL AND ENVIRONMENTS 1

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

University of California, Berkeley College of Engineering

Structure and Interpretation of Computer Programs

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

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

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

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

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

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Midterm #1 Fall minutes

CS 2316 Exam 4 Fall 2011

CS 111X - Fall Test 1

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Version B Final Exam

C ONTROL AND H IGHER O RDER F UNCTIONS

STREAMS, ITERATORS, AND BINARY TREES 10

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

EXAMINATION INSTRUCTIONS

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

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

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

Lecture #2: Programming Structures: Loops and Functions

CS150: Exam 2 Due: Mon Apr 20th by 3:30pm (in class) or by 6:30pm (OLS 219) UVA ID: Directions

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CS 1301 Exam 1 Fall 2010

Practice midterm exam

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

Final Exam Version A

CS 115 Exam 3, Spring 2014

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

TUPLES AND RECURSIVE LISTS 5

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

CS3 Midterm 2 Summer 2008

Structure and Interpretation of Computer Programs

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

CS 113 MIDTERM EXAM 2 SPRING 2013

RECURSION AND LINKED LISTS 3

15-110: Principles of Computing, Spring 2018

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

CS150 Sample Final. Name: Section: A / B

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

Multiple-choice (35 pt.)

Transcription:

CS 61A Summer 2016 Structure and Interpretation of Computer Programs Midterm Solutions INSTRUCTIONS You have 2 hours and 50 minutes to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except one 8.5 11 cheat sheet of your own creation. Mark your answers on the exam itself. We will not grade answers written on scratch paper. Last name First name Student ID number Instructional account (cs61a-_) BearFacts 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. (6 points) Olympic Games (a) (4 pt) For each of the statements below, write the output displayed by the interactive Python interpreter when the statement is executed. The output may have multiple lines. No answer requires more than four lines. The first two have been provided as examples. Assume that you have started python3 and executed the following statements: gold = [0, 1] def go(dream, team ): print ( Steps to success : ) def medal ( lion ): return dream ( team, lion ) return medal def win ( big ): print ( Eat vegetables. ) return big [ -1] + big [ -2] def get (it, done ): do = it while done < 2: print (" Don t cheat!") do, done = do + [ win (do )], done + 1 return do >>> print (4, 5) + 1 >>> riooo = go( get, gold ) 4 5 Steps to success : Error >>> gold [ -2] >>> usa = riooo (0) 0 Don t cheat! Eat vegetables. Don t cheat! Eat vegetables. >>> win ( gold ) >>> usa Eat vegetables. [0, 1, 1, 2] 1 (b) (2 pt) What would be the return value of riooo(-2), after evaluating the expressions above? [0, 1, 1, 2, 3, 5]

Name: 3 2. (6 points) The Name Game 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 frames. Add all missing values created or referenced during execution. Show the return value for each local frame. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 def person(name, age): def brain(ask): if ask == 'name': return name elif ask == 'age': return age return brain def name(guy): return guy('name') def age(gal): return gal('age') my_name, my_age = 'Marv', 21 marv = person(my_name, my_age) dan = person('dan', age(marv) + 7) Global person name age my_name 'Marv' my_age 21 marv dan f1: person [parent= ] Global name 'Marv' func person(name, age) [parent=global] func name(guy) [parent=global] func age(gal) [parent=global] func brain(ask) [parent=f1] age 21 brain Return Value f2: age [parent= ] Global gal Return Value 21 f3: brain [parent= ] f1 ask 'age' Return Value 21 f4: person [parent= ] Global func brain(ask) [parent=f4] name age 'Dan' 28 brain Return Value

4 3. (3 points) High Scores Write expressions involving the list lst that evaluate to the following values. Your expressions must use lst. For the second and third lines, you must use a list comprehension. >>> lst = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] >>> len(lst) 5 >>> [[11 - x, 11 - y] for x, y in lst] [[10, 9], [8, 7], [6, 5], [4, 3], [2, 1]] >>> [2 * x for x in lst[0]] [2, 4] 4. (4 points) Kerbal Space Program Define the function is sorted that takes in a non-negative integer n and returns if the digits of n are in non-increasing order from left to right, and False otherwise. See the doctests for details. You may only use the lines provided. You may not use any Python built-in sorting functions. def is_sorted (n): Returns whether the digits in n are in non - increasing order from left to right. >>> is_ sorted (4) >>> is_ sorted ( 55555) >>> is_ sorted ( 9876543210) >>> is_ sorted ( 9087654321) False if n < 10: return elif n % 10 > (n // 10) % 10: return False else : return is_ sorted ( n // 10)

Name: 5 5. (6 points) Katamari (a) (4 pt) Define the function aggregate that takes in a two-argument function fn, a sequence seq, and a predicate function pred and returns the result of using fn to combine the elements in seq for which pred returns. See the doctests for details. You may assume fn is commutative and never returns None. You may only use the lines provided. You may not need to fill all the lines. You may not use an import statement. def aggregate ( fn, seq, pred ): Aggregates using fn the elements in seq that satisfy pred. >>> def is_even (x):... return x % 2 == 0 >>> def sum_plus_one (x, y):... return x + y + 1 >>> aggregate ( sum_ plus_ one, [2, 4, 6], is_ even ) # (2 + 4 + 1) + 6 + 1 14 >>> # If no elements satisfy pred, return None >>> aggregate ( sum_plus_one, [1, 3, 5, 7, 9], is_even ) >>> # If only one element satisfies pred, return that element >>> aggregate ( sum_plus_one, [1, 2, 3], is_even ) 2 result = None for elem in seq : if pred ( elem ): if result == None : result = elem else : result = fn( result, elem ) return result (b) (2 pt) Use the aggregate function above, which you can assume works correctly, to define the fact function that returns the factorial of non-negative integers. Use only one line of code. If you need more space, you can continue your line of code on the second blank. The add and mul functions, which may be useful, have been imported for you. Hint: You may need the ternary operator <expr1> if <cond> else <expr2>. >>> from operator import add, mul >>> fact = lambda n: aggregate(mul, range(1, n+1), lambda x: ) if n > 0 else 1 >>> fact(0) 1 >>> fact(5) 120

6 6. (6 points) Legend of Zelda This question uses the following linked list data abstraction. We have provided an example linked list. >>> empty = empty >>> link (1, link (2, link (3))) >>> def link ( first, rest = empty ):... return [ first, rest ]... >>> def first ( lnk ):... return lnk [0]... >>> def rest ( lnk ):... return lnk [1] Define the function linked sum that takes in a linked list of positive integers lnk and a non-negative integer total and returns the number of combinations of elements in lnk that sum up to total. You may use each element in lnk zero or more times. See the doctests for details. Do not violate abstraction barriers. You may only use the lines provided. You may not need to fill all the lines. def linked_sum (lnk, total ): Return the number of combinations of elements in lnk that sum up to total. >>> # Four combinations : 1 1 1 1, 1 1 2, 1 3, 2 2 >>> linked_sum ( link (1, link (2, link (3, link (5)))), 4) 4 >>> linked_sum ( link (2, link (3, link (5))), 1) 0 >>> # One combination : 2 3 >>> linked_sum ( link (2, link (4, link (3))), 5) 1 if total == 0: return 1 elif lnk == empty or total < 0: return 0 else : with_ first = linked_ sum ( lnk, total - first ( lnk )) without_first = linked_sum ( rest ( lnk ), total ) return with_ first + without_ first

Name: 7 7. (9 points) Game of Thrones This question uses the following tree data abstraction. We have provided an example tree. >>> def tree ( entry, children =[]): >>> tree (3, [... return [ entry, children ] tree (1),... tree (2, [ >>> def entry ( tree ): tree (1), tree (1)... return tree [0] ])])... >>> def children ( tree ):... return tree [1] (a) (6 pt) Define the function track lineage that takes in a tree of strings family tree and a string name. Assume that there is a unique node with entry name. track lineage returns a list with the entries of the parent and grandparent of that node. 1 If the node with entry name does not have a parent or grandparent, return None for that element in the list. See the doctests for details. Do not violate abstraction barriers. You may only use the lines provided. You may not need to fill all the lines. def track_ lineage ( family_ tree, name ): Return the entries of the parent and grandparent of the node with entry name in family_ tree. >>> t = tree ( Tytos, [... tree ( Tywin, [... tree ( Cersei ), tree ( Jaime ), tree ( Tyrion )... ]),... tree ( Kevan, [... tree ( Lancel ), tree ( Martyn ), tree ( Willem )... ])]) >>> track_lineage (t, Cersei ) [ Tywin, Tytos ] >>> track_lineage (t, Tywin ) [ Tytos, None ] >>> track_lineage (t, Tytos ) [None, None ] def tracker (t, p, gp ): if name == entry (t): return [p, gp] for c in children (t): res = tracker (c, entry (t), p) if res : return res return tracker ( family_ tree, None, None ) 1 The grandparent of a node is the parent of the node s parent.

8 (b) (3 pt) Assuming that track lineage works correctly, define the function are cousins that takes in a tree of strings family tree and two strings name1 and name2 and returns if the node with entry name1 and the node with entry name2 are cousins in family tree. Assume that there are unique nodes with entries name1 and name2 in family tree. See the doctests for details. Two nodes are cousins if they have the same grandparent but different parents. You may only use the lines provided. You may not need to fill all the lines. def are_ cousins ( family_ tree, name1, name2 ): Return if a node with entry name1 is a cousin of a node with entry name2 in family_ tree. >>> are_ cousins ( t, Kevan, Tytos ) # same tree as before False >>> are_cousins (t, Cersei, Lancel ) >>> are_cousins (t, Jaime, Lancel ) >>> are_cousins (t, Jaime, Tyrion ) False p1, gp1 = track_ lineage ( family_ tree, name1 ) p2, gp2 = track_ lineage ( family_ tree, name2 ) return p1!= p2 and gp1 is not None and gp1 == gp2 8. (0 points) Games of Berkeley In the box below, write a positive integer. The student who writes the lowest unique integer will receive one extra credit point. In other words, write the smallest positive integer that you think no one else will write.