Structure and Interpretation of Computer Programs Spring 2019 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 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

Structure and Interpretation of Computer Programs

LINKED LISTS AND MIDTERM REVIEW

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

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

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

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 2017 Mock Midterm 1

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

LINKED LISTS AND MIDTERM REVIEW 6

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, 2019 Solutions. 1 Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

Structure and Interpretation of Computer Programs

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

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

Structure and Interpretation of Computer Programs Spring 2014 Test 2

CS 111X - Fall Test 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Spring 2017 CS 1110/1111 Exam 1

Structure and Interpretation of Computer Programs

Solutions to CS 61A Challenge Problems: Midpoint Review

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

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

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

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

Structure and Interpretation of Computer Programs

Trees & Sequences Spring 2018 Discussion 3: February 14, 2018 Solutions. 1 Sequences and Lists

Midterm Exam 1 Solutions

CSM Mock Final Spring 2018

Structure and Interpretation of Computer Programs

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

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

RLISTS AND THE ENVIRONMENT MODEL 9

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

Structure and Interpretation of Computer Programs

CS 111X - Spring Final Exam - KEY

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

CS Prelim 2 Review Fall 2018

61A Lecture 21. Friday, March 13

Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA

Midterm #1 Fall minutes

TREES AND ORDERS OF GROWTH 8

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

1 Fall 2017 CS 1110/1111 Exam 3

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

Final Exam Version A

Spring 2017 CS 1110/1111 Exam 2

FINAL REVIEW 13. December 4, Sample Final Questions

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

Introduction to Python! Lecture 2

Midterm I - Solution CS164, Spring 2014

TREES AND ORDERS OF GROWTH 7

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

CS 540: Introduction to Artificial Intelligence

61A Lecture 2. Wednesday, September 4, 2013

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

STREAMS, ITERATORS, AND BINARY TREES 10

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

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

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

Transcription:

CS 61A Structure and Interpretation of Computer Programs Spring 2019 Midterm 2 INSTRUCTIONS ˆ You have 2 hours to complete the exam. ˆ The exam is closed book, closed notes, closed computer, closed calculator, except two hand-written 8.5" 11" crib sheets of your own creation and the ocial 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) POLICIES & CLARIFICATIONS ˆ If you need to use the restroom, bring your phone and exam to the front of the room. ˆ You may use built-in Python functions that do not require import, such as min, max, pow, len, abs, sum, next, iter, list, tuple, map, filter, zip, all, and any. ˆ You may not use example functions dened on your study guides unless a problem clearly states you can. ˆ For ll-in-the blank coding problems, we will only grade work written in the provided blanks. You may only write one Python statement per blank line, and it must be indented to the level that the blank is indented. ˆ Unless otherwise specied, you are allowed to reference functions dened in previous parts of the same question. ˆ You may use the Tree, Link, and BTree classes dened on Page 2 (left column) of the Midterm 2 Study Guide.

2 1. (12 points) What Would Python Display 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. If evaluation would run forever, write Forever. To display a function value, write Function. The rst two rows have been provided as examples. The interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have rst started python3 and executed the statements on the left. items, n = [], 2 class Airpods: cost, k = 200, 0 f = lambda self: print(self) def init (self): Airpods.k += 1 Airpods.f(self) items.extend([self]) Expression pow(10, 2) 100 print(link(2, Link(3))) <2, 3> TwoAirpods.cost lost = Airpods() Interactive Output def repr (self): return (Airpods.k < 2 and "lonely") or "pair" class TwoAirpods(Airpods): def init (self): self.k = 2 Airpods. init (self) Airpods. init (self) willbelost = TwoAirpods() str(lost) def discount(a): a.cost //= 2 def u(w, u): return [print(u) for u in [w, u]] discount(airpods) [item.k for item in items] u(lost, willbelost)

Name: 3 2. (10 points) Ultimate Fill in the environment diagram that results from executing the code on the right until the entire program is nished, an error occurs, or all frames are lled. You may not need to use all of the spaces or frames. A complete answer will: ˆ Use box-and-pointer notation for all lists. ˆ Add missing names and parents to all local frames. ˆ Add 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 defeated = ['luigi'] def start(): chal = 'falco' def approach(new_chal): nonlocal chal defeated.append(chal) chal = new_chal def revive(): return defeated.pop() return [approach, revive] gameover = start()[:] gameover.pop(0)( marth') restart = gameover[0]() Global frame func start() [parent=global] func approach(new_chal) [parent= ] f1: [parent= ] Return Value func revive() [parent= ] f2: [parent= ] Return Value f3: [parent= ] Return Value

4 3. (5 points) Deep lists Implement in_nested which takes in a value v and a nested list or an individual value L and returns whether the value is contained in the list. Hint: The built-in function type takes an object and returns the type of that object. def in_nested(v, L): >>> in_nested(5, [1, 2, [[3], 4]]) False >>> in_nested(9, [[[1], [6, 4, [5, [9]]], 7], 7, 7]) True >>> in_nested(1, 1) True return else: return

Name: 5 4. (8 points) A data structure by any other form would smell just as sweet... (a) (6 pt) Implement link_to_dict which takes a linked list encoding a attened dictionary (in which elements are key1 value1 key2 value2 key3 value3, etc), removes all the values, and returns the equivalent dictionary. The input and returned list may include duplicate keys, as in the example below. You may assume the linked list always contains an even number of elements. The Link class is provided below. class Link: empty = () def init (self, first, rest=empty): self.first = first self.rest = rest def str (self): string = '<' while self.rest is not Link.empty: string += str(self.first) + ', ' self = self.rest return string + str(self.first) + '>' def link_to_dict(l): >>> L = Link(1, Link(2, Link(3, Link(4, Link(1, Link(5)))))) >>> print(l) <1, 2, 3, 4, 1, 5> >>> link_to_dict(l) {1: [2, 5], 3: [4]} >>> print(l) <1, 3, 1> D = {} while : key, value = else: return D (b) (2 pt) Circle the Θ expression that describes the number of iterations of the while loop in link_to_dict where n is the length of the list. Θ(1) Θ(log n) Θ(n) Θ(n 2 ) Θ(2 n ) None of these

6 5. (10 points) I speak for the Trees Execute each line of code in order. If a line errors, assume we didn't type that line. For each line, indicate whether it was: ˆ (E) an error ˆ (D) a Data Abstraction Violation ˆ (OK) perfectly ne code. Please ll in the bubbles completely. E D OK t = Tree(3, [Tree(1), Tree(2)]) E D OK L = [tree(1)] E D OK L.append(tree(2)) E D OK s = tree(3, L) def tree(label, branches=[]): return [label] + list(branches) def label(tree): return tree[0] def branches(tree): return tree[1:] E D OK s E D OK print(s) E D OK t E D OK t.label def print_tree_adt(t, indent=0): print(' ' * indent + str(label(t))) for b in branches(t): print_tree_adt(b, indent + 1) E D OK print(t) E D OK label(s) class Tree: def init (self, label, branches=[]): self.label = label self.branches = list(branches) def str (self): return '\n'.join(self.indented()) def indented(self, k=0): ind = [] for b in self.branches: for line in b.indented(k + 1): ind.append(' ' + line) return [str(self.label)] + ind E D OK t.label = 4 E D OK label(s) = 4 E D OK s[0] = 5 E D OK print_tree_adt([5]) E D OK print_tree_adt(s) E D OK t.branches[0].label = s E D OK print_tree_adt(t.branches[0]) E D OK print_tree_adt(t.branches[0].label) E D OK t E D OK print(t)

Name: 7 6. (15 points) Trie this A Trie is a Tree where every node in the tree contains a single letter except for the root which is always the empty string. Every path from the root to a leaf forms a word. You may assume no words are substrings of other words in the trie (e.g., hi and him). The gure below is a trie generated by storing the words [this, is, the, trie]. The Tree class is dened on Page 2 of the Midterm 2 Study Guide. (a) (7 pt) Implement add_word which takes a Trie and a word and adds the word to the trie. def make_trie(words): Makes a tree where every node is a letter of a word. All words end as a leaf of the tree. words is given as a list of strings. trie = Tree('') for word in words: add_word(trie, word) return trie "e" "h" "i" "" "t" "i" "r" "s" "i" "s" "e" def add_word(trie, word): return branch = for : branch = branch = (b) (8 pt) Implement get_words, which takes a Trie and returns a list of all the words the Trie is storing. def get_words(trie): >>> get_words(make_trie(['this', 'is', 'the', 'trie'])) ['this', 'the', 'trie', 'is'] return return sum(, [])

8 No more questions.