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

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

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

OOP 1 OOP CS 61A GROUP MENTORING. July 19, 2017

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Sample Final Exam Questions

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 2

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

Structure and Interpretation of Computer Programs

Final Exam Version A

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

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

Structure and Interpretation of Computer Programs Summer 2015 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

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

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

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

CS 314 Principles of Programming Languages. Lecture 16

Structure and Interpretation of Computer Programs

CS 115 Exam 2 (Section 1) Fall 2017 Thu. 11/09/2017

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CS 61A Discussion 8: Scheme. March 23, 2017

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

CS3 Midterm 2 Summer 2008

Structure and Interpretation of Computer Programs Spring 2015 Midterm 2

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

Structure and Interpretation of Computer Programs

ITERATORS AND STREAMS 9

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

15-110: Principles of Computing Sample Exam #1

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Guerrilla Section 7: Macros, SQL SQL

CS 314 Principles of Programming Languages

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

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

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

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Introduction to Functional Programming

Exercise 1: Graph coloring (10 points)

GENERATORS AND STREAMS

CS 115 Exam 3, Spring 2014

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

CS 1301 CS1 with Robots Summer 2007 Exam 1

CSE413 Midterm. Question Max Points Total 100

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

CS115 - Module 10 - General Trees

CS 111X - Fall Test 1

INTERPRETATION AND SCHEME LISTS 12

CS 3 Midterm 1 Review

6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

Structure and Interpretation of Computer Programs

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Lisp. Versions of LISP

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 111X - Spring Final Exam - KEY

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

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

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

Fall 2017 December 4, Scheme. Instructions

Scheme: Strings Scheme: I/O

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

CSE341 Autumn 2017, Final Examination December 12, 2017

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

FINAL REVIEW 13. December 4, Sample Final Questions

SCHEME AND CALCULATOR 5b

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Transcription:

CS 61A Summer 2018 Structure and Interpretation of Computer Programs Mock Final INSTRUCTIONS You have 80 minutes to complete the exam individually. The exam is closed book, closed notes, closed computer, and closed calculator, except for two hand-written 8." 11" crib sheet of your own creation. Mark your answers on the exam itself. We will not grade answers written on scratch paper. Last (Family Name First (Given Name Student ID Number Berkeley Email Teaching Assistant Alex Stennet Alex Wang Cameron Malloy Chae Park Chris Allsman Christina Zhang Derek Wan Erica Kong Griffin Prechter Jemin Desai Jennifer Tsui Jenny Wang Kevin Li Nancy Shaw All the work on this exam is my own. (please sign POLICIES & CLARIFICATIONS You may use built-in Python functions that do not require import, such as min, max, pow, and abs. For fill-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 specified, you are allowed to reference functions defined in previous parts of the same question. The topics covered in this mock exam are not comprehensively representative of the topics that will appear on the actual final exam.

2 1. (6 points Retrieve the output 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. Each expression has at least one line of output. If an error occurs, write Error, but include all output displayed before the error. To display a function value, write Function. If an expression would take forever to evaluate, write Forever. The interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have started python3 and executed the code shown on the left first, and then you evaluate each expression on the right in the order shown. Expressions evaluated by the interpreter have a cumulative effect. class Retriever: height = def init (self, boy: self.good = boy def bark(self: print('woof!' return self.height - 2 def fetch(self, other: return 'Fetch!' def str (self: return 'I retrieve things' class Golden(Retriever: name = 'Golden' height = 6 def init (self, fluffy, boy: self.fluffy = fluffy self.boy = boy def bark(self, dog: print(dog.name + ' says hi!' return Retriever.bark(self def sniff(self, friend: for _ in range(self.boy: print(friend class Labrador(Retriever: name = 'Labrador' height = def init (self, name: self.name = name self.d = Golden.bark(self, self def fluffy(self, fluff: return fluff def fetch(self, other: return self.fluffy(other def repr (self: return 'A pup!' goldie = Golden(lambda x: 10, Expression print(, + 1 goldie.fluffy(goldie goldie.fetch(labrador lucy = Labrador('Lucy' lucy.fetch(labrador('olly' goldie.boy = lucy.d goldie.sniff(lucy.fetch(goldie Retriever.bark(goldie Interactive Output Error

Name: 3 2. (6 points Schemin For each of the expressions in the table below, write the output displayed by the interactive Scheme interpreter when the expression is evaluated. The output may have multiple lines. Each expression has at least one line of output. If an error occurs, write Error, but include all output displayed before the error. The interactive interpreter displays the value of a successfully evaluated expression, unless it is None. Assume that you have started our implementation of the Scheme interpreter with python3 scheme (or, equivalently, the interpreter at scheme.cs61a.org and executed the code shown on the left first, and then you evaluate each expression on the right in the order shown. Expressions evaluated by the interpreter have a cumulative effect. (define (mischief x f g. args (if (even? x (apply f args (apply g args (define (mystery a b (if (= a b 'okay (begin (print 'hmmm (mystery (- a 1 (+ b 1 (define s (cons-stream 1 (cons-stream (mystery 1 nil Expression (+ (print 1 (mystery 3 1 (mischief (+ 1 3 cons list 1 2 (cons (cons 6 (cons 1 nil '(a `(list,(cons 1 2 (list 3 (define rest (cdr-stream s (car (cdr-stream s Interactive Output Error 3. ( points Zero Write a macro called zero-cond that takes in a list of clauses, where each clause is a two-element list containing two expressions, a predicate and a corresponding result expression. All predicates evaluate to a number. The macro should evaluate each predicate and return the value of the expression corresponding to the first true predicate, treating 0 as a false value. scm> (zero-cond ((0 'result1 ((- 1 1 'result2 ((* 1 1 'result3 (2 'result result3 (define-macro (zero-cond clauses (cons 'cond (map

. (6 points DoubleTree The following questions use this implementation of the Tree class: class Tree: def init (self, label, branches=[]: def str (self: for b in branches: Represents trees in a readable format. assert isinstance(b, Tree >>> print(tree(1, [Tree(2] self.label = label 1 self.branches = list(branches 2 def is_leaf(self: return not self.branches... (a (2 pt Fill in the definition of copy_tree below, which takes in a Tree instance t and returns a new tree object that contains the same items as t. def copy_tree(t: >>> t1 = Tree(1, [Tree(2] >>> t2 = copy_tree(t1 >>> print(t2 1 2 >>> t1 is t2 False >>> t1.branches is t2.branches False return (b ( pt Now, use copy_tree to write the function double_tree. This function takes in a tree and mutates it by duplicating every branch at every level in the tree. Assume that the copy_tree function works as expected, regardless of what you wrote above. def double_tree(t: >>> t = Tree(3, [Tree(, [Tree(]] >>> double_tree(t >>> print(t 3 >>> t.branches[0].label = 6 # Make sure to copy branches instead of repeating them! >>> t.branches[1].label # Changing original branch label doesn't affect new branch [ for in ] t.branches.

Name:. (6 points Don t repeat yourself (a (2 pt Implement repeater, which takes in a list of positive numbers and returns a list where every number in the original list except for the first number appears a number of times equivalent to the previous number. scm> (repeater nil ( scm> (repeater '(1 2 3 (2 3 3 scm> (repeater '( 1 2 (1 1 1 1 2 (define (repeater nums (define (repeat nums n (cond ( ((= n 0 (else (repeat (b ( pt Implement zip-tail, which is a tail recursive procedure that takes in two lists a and b and returns a single list containing two-element lists of co-indexed elements from a and b. If one list is shorter than the other, the zipped list s length is that of the shorter list. Your solution should be tail recursive. scm> (zip-tail '(1 2 3 '( 6 ((1 (2 (3 6 scm> (zip-tail '(c 6 a '(s 1! hello world ((c s (6 1 (a! Hint: Use the built-in append procedure, which you can assume is tail recursive, to concatenate two lists together. For example: scm> (append '(1 2 3 '( 6 (1 2 3 6 (define (zip-tail a b (define (zipper (if

6 6. (7 points Teaqual Chae and Jennifer decide to open a tea house, called ADTeas, where customers can build their own custom drinks. A drink is defined as some combination of a tea from the teas table as the base, a syrup from the syrups table, and a topping from the toppings table. Each tea variety belongs to some tea type, either green, black, oolong, or white. Each syrup has a popularity level from 1- ( being the most popular. Each topping has a particular tea type that it complements the most as well as a popularity level from 1-. teas tea tea_type jasmine green high mountain oolong silver needle white assam black osmanthus oolong gong fu black syrups syrup popularity honey 3 mango peach passionfruit grapefruit 2 toppings topping tea_type popularity lychee jelly green tapioca pearl black milk pudding oolong red bean green 2 grass jelly white 3 (a (2 pt Tammy needs help deciding what drink to get at ADTeas. She has no preference for the tea base or syrup, but only wants drinks with toppings that complement the type of the tea base and with combinations of toppings and syrups that have a combined average popularity of at least.. Create a table called tammys_drinks, which contains all drinks that Tammy would like given by some tea, a syrup, and a topping. CREATE TABLE tammys_drinks AS SELECT AS tea, AS syrup, AS topping FROM WHERE ; (b ( pt Jennifer creates the table special_drinks to represent a special menu of popular drink combinations. It has columns, each representing a tea base for the drink, a syrup, a topping, and a popularity value, which is the average of the popularity values of the topping and syrup in the drink. CREATE TABLE special_drinks(tea, syrup, topping, popularity; Tammy decides to have office hours at ADTeas and shares her drink choices with the students, which they love! Insert one drink of each tea type from tammys_drinks into special_drinks. Specifically, for each tea type, insert the drink that has the highest topping and syrup popularity average value out of all of the drinks of that type in tammys_drinks. Assume there is at most one drink of each tea type that fits this description. INSERT INTO special_drinks SELECT d.tea, d.syrup, d.topping, FROM tammys_drinks AS d, WHERE GROUP BY ; (c (1 pt Chae notices that not many people are purchasing drinks with grass jelly and that red bean is becoming more popular. She wants to remove grass jelly as an topping option to cut costs and raise red bean s popularity level to 3. Fill in the statements below to reflect this. DELETE FROM WHERE ; UPDATE toppings SET WHERE ;