Structure and Interpretation of Computer Programs

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

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

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

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

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

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Spring 2017 CS 1110/1111 Exam 2

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Spring 2017 CS 1110/1111 Exam 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS 111X - Fall Test 1

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Structure and Interpretation of Computer Programs

CS 111X - Spring Final Exam - KEY

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 Spring 2014 Final (with corrections)

CS 1301 Exam 1 Spring 2011

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CS 1301 Exam 1 Spring 2014

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

COS 126 General Computer Science Spring Written Exam 1

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CS 2316 Exam 1 Spring 2014

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

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

CS 2316 Exam 4 Fall 2011

Structure and Interpretation of Computer Programs

Midterm I Practice Problems

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 1 Spring 2014

Short Answer Questions (40 points)

Midterm #1 Fall minutes

CS 1301 Exam 3 Spring 2014

CS 2316 Exam 1 Spring 2013

Your (printed!) Name: CS 1803 Exam 3. Grading TA / Section: Monday, Nov. 22th, 2010

UVa ID: NAME (print): CS 4501 LDI Midterm 1

Structure and Interpretation of Computer Programs Spring 2014 Test 2

Spring 2017 CS 1110/1111 Exam 3

Structure and Interpretation of Computer Programs

Version B Final Exam

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

CPSC 217 Midterm (Python 3 version)

CONTROL AND ENVIRONMENTS 1

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2014

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

15-110: Principles of Computing Sample Exam #1

CS 1301 Exam 2 Fall 2010

CSE 131 Introduction to Computer Science Fall Exam I

Notices. Test rules. Page 1 of 8. CS 1112 Spring 2018 Test 2

All written answers are limited to their question boxes. Make sure all answers are easily legible.

Midterm #2a Spring minutes

CS 1301 CS1 with Robots Summer 2007 Exam 1

Structure and Interpretation of Computer Programs

CS 1301 Exam 1 Fall 2014

CS Prelim 2 Review Fall 2018

CS 1301 Exam 1 Fall 2010

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS Name : Grading TA:

CS 1301 Exam 1 Spring 2015

Structure and Interpretation of Computer Programs

(the bubble footer is automatically inserted into this space)

ENVIRONMENT DIAGRAMS AND RECURSION 2

C ONTROL AND H IGHER O RDER F UNCTIONS

Midterm Exam 1 Solutions

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

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

CS 1301 Exam 1 Fall 2011

Transcription:

CS 61A Spring 2017 Structure and Interpretation of Computer Programs Test 1 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 (implementations not shown) empty =... # The empty list def link(first, rest): """A linked list whose first element is FIRST and the linked list REST is the rest of the list.""" def first(lnklst): """The first item in linked list LNKLST.""" def rest(lnklst): """The list following the first item in linked list LNKLST.""" def isempty(lnklst): """True if LNKLST is empty.""" def print_link(lnklst): """Prints the linked list LNKLST in the format (v0, v1,...)."""

Name: 3 1. (12 points) Evaluate These! 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. If an expression yields (or prints) a function, write <Function>. No answer requires more than 3 lines. (It s possible that all of them require even fewer.) The first two rows have been provided as examples. The interactive interpreter displays the value of a successfully evaluated expression, unless it is None, plus all values passed to print. Assume that python3 has executed the statements on the left: def w(l): if len(l) == 0: return L elif L[0] in L[1:]: return w(l[1:]) else: return [L[0]] + w(l[1:]) def reduce(f, L, init): if len(l) == 0: return init else: return reduce(f, L[1:], f(init, L[0])) Expression Interactive Output pow(2, 3) 8 print(4, 5) + 1 4 5 Error 1 + (4 and 6) + (5 or 0) + (0 and 8) 12 f = lambda x: x g = lambda y: f(g) g(2)(2) 0 and print(2) range(1,20)[-2] w([1, 2, 3, 1, 8, 2]) f = lambda x: lambda y: \ lambda z: x+y+z reduce(lambda g, x: g(x), [1, 2, 3], f) <Function> 0 18 [3,1,8 2] 6

4 2. (12 points) Environmental Policy (a) (6 pt) 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, frames, or blank lambda function values. A complete answer will: Add all missing names and parent annotations to all local frames. Add all missing values created or referenced during execution. Show the return value for each local frame. 1 2 3 4 5 def h(y): return g(lambda: y) def g(f): return f()() h(lambda: 3) Global frame h g func h(y) [parent=global] func g(f) [parent=global] f1: h [parent=global] func λ () <line 5> [parent=global] y func λ () <line 2> [parent=f1] f2: g [parent=global] f f3: λ <line 2> [parent= f1] Return value f4: λ <line 5> [parent= Global]

Name: 5 (b) (6 pt) The environment diagram below corresponds to the execution of a certain program. The frames shown were created in top-to-bottom order, and at one point they were all simultaneously active (their functions had not returned). The diagram shows the situation right after all the functions have returned. Write a Python program whose execution corresponds to this environment diagram. Many answers are possible, but as a guideline, fewer than 10 lines are really needed. In any case, your answer must not create extra frames or additional functions. Global frame a k z 3 func a() [parent=global] func k(b) [parent=global] f1: a [parent=global] g func g() [parent= f1] f2: k [parent=global] b f3: g [parent=f1] Write solution here. def a(): def g(): return 3 return k(g) def k(b): return b() z = a()

6 3. (4 points) Sequence Checking Fill in the following function so that it fulfills its comment. def make_checker(relation, start, end): """Assumes that START and END are integers and RELATION is a two-argument function that returns true/false values. Returns a function that, given a function f as input, returns True if RELATION returns true for all adjacent values in the sequence f(start), f(start+1),... f(end-1). For example, eq_chk, below, checks that the values returned by its argument function for values 0-4 are all equal, while up_chk checks that the values returned by the argument function are in strictly increasing order. >>> eq_chk = make_checker(lambda x, y: x == y, 0, 5) # Check all equal >>> eq_chk(lambda x: 3) True >>> eq_chk(lambda x: x) False >>> up_chk = make_checker(lambda x, y: x < y, 0, 5) # Check increasing >>> up_chk(lambda x: x) True >>> up_chk(lambda x: 3) False """ def checker(f): for k in range(start + 1, end): if not relation(f(k-1), f(k)): return False return True return checker

Name: 7 4. (1 points) Extra In the formula a = 0.4+0.3 2 m (m = inf,0,1,2,...), what does a refer to? The approximate positions of planets (in AU). This is known as Bode s Law. 5. (4 points) Insert Fill in the following function to fulfill its comment. The linked-list interface that you should use is given on page 2. Warning: this problem deals with linked lists, NOT Python lists or tuples. You cannot use +, len(), indexing (L[k]), or list construction ([...]). def link_insert(lnklst, value, before): """Return a linked list identical to LNKLST, but with VALUE inserted just before the first occurrence of BEFORE in the list, if any. The returned list is identical to LNKLST if BEFORE does not occur in LNKLST. The operation is non-destructive. >>> L = link(2, link(3, link(7, link(1)))) >>> print_link(l) (2, 3, 7, 1) >>> Q = link_insert(l, 19, 7) >>> print_link(q) (2, 3, 19, 7, 1) >>> print_link(link_insert(l, 19, 20)) (2, 3, 7, 1) """ if isempty(lnklst): return lnklst elif first(lnklst) == before: return link(value, lnklst) else: return link(first(lnklst), link_insert(rest(lnklst), value, before))

8 6. (4 points) Longest Nondecreasing Suffix Consider a function up_suffix that is supposed to return the longest suffix of a Python list of integers such that the suffix consists of nondecreasing values. For example, when applied to [1, 2, 3, 4, 5, 1, 3, 3, 4] it should yield [1, 3, 3, 4]. Fill in the following function to do this: def up_suffix(l): """Returns the longest non-descending suffix of Python list L. """ def longest_suffix_start(l): """The index in L of the beginning of the longest nondecreasing suffix of L. For the empty list, returns 0. >>> longest_suffix_start([1, 2, 3, 4, 5, 1, 3, 3, 4]) 5 >>> longest_suffix_start([2, 4, 6, 8, 10]) 0 """ k = len(l) - 1 while k > 0 and L[k-1] <= L[k]: k -= 1 return max(k, 0) # We were not fussy about people who returned -1 for the empty list, # so just return k was all right as well. return L[longest_suffix_start(L):]

Name: 9 7. (4 points) Post s Problem Consider two Python lists of strings, where the two lists have equal length, N: A = [ "a", "ab", "bba"] B = [ "baa", "aa", "bb" ] Is there a sequence of integers i 1, i 2,...i m where m > 0 and 0 i k < N for all k, such that A[i 1 ]+A[i 2 ]+ +A[i m ] = B[i 1 ]+B[i 2 ]+ +B[i m ]? This is called the Post Correspondence Problem. For this A and B, the answer is yes for m = 4: (2, 1, 2, 0), because A[2] + A[1] + A[2] + A[0] == "bba" + "ab" + "bba" + "a" == "bbaabbbaa" B[2] + B[1] + B[2] + B[0] == "bb" + "aa" + "bb" + "baa" == "bbaabbbaa" On the other hand, the answer is no if we limit m to 3 (so we cannot add more than three strings), or if we shorten the lists by removing A[0] and B[0]. Fill in the following function to determine whether there is a solution. def correspond(a, B, M): """Assuming A and B are lists of strings with len(a) == len(b), and M is an integer, returns true iff there is a sequence of indices into A and B, (i1, i2,..., im), where 1 <= m <= M, such that A[i1] + A[i2] +... + A[im] == B[i1] + B[i2] +... + B[im]. """ N = len(a) def can_correspond(sa, sb, M): """Return true iff there is some sequence of indices into A and B, (i1, i2,..., im), where 1 <= m <= M, such that SA + A[i1] + A[i2] +... + A[im] == SB + B[i1] + B[i2] +... + B[im]. Assumes M is a non-negative integer. """ if M <= 0: return False else: for i in range(n): ta = sa + A[i] tb = sb + B[i] if ta == tb: return True elif can_correspond(ta, tb, M - 1): return True return False return can_correspond("", "", M)