CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

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

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

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

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Scheme Basics > (butfirst '(help!)) ()

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

YOUR NAME PLEASE: *** SOLUTIONS ***

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


Statistics Case Study 2000 M. J. Clancy and M. C. Linn

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

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

CS61A Midterm 2 Review (v1.1)

User-defined Functions. Conditional Expressions in Scheme

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

CSE413 Midterm. Question Max Points Total 100

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

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

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

SCHEME AND CALCULATOR 5b

CS 275 Name Final Exam Solutions December 16, 2016

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

6.001 Notes: Section 8.1

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

Repetition Through Recursion

Structure and Interpretation of Computer Programs

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CSE341 Autumn 2017, Final Examination December 12, 2017

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Richard Feynman, Lectures on Computation

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

CSE341 Spring 2017, Final Examination June 8, 2017

Structure and Interpretation of Computer Programs

Introduction to Scheme

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

CS450 - Structure of Higher Level Languages

Structure and Interpretation of Computer Programs

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Section 05: Solutions

Streams and Evalutation Strategies

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Section 05: Solutions

CSE341 Spring 2017, Final Examination June 8, 2017

Structure and Interpretation of Computer Programs

CS 104 (Spring 2014) Final Exam 05/09/2014

University of Massachusetts Lowell

6.001 Notes: Section 31.1

Largest Online Community of VU Students

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

XC Total Max Score Grader

A random five-digit 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 1 2 3


CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Discussion 11. Streams

CS 3 Midterm 1 Review

The components of a tree are called nodes. At the top is the root node of the tree; in the

Warm-up and Memoization

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Lists in Lisp and Scheme

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Functional Programming - 2. Higher Order Functions

CS 360 Programming Languages Interpreters

Introduction to Functional Programming

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Discussion 4. Data Abstraction and Sequences

Scheme Quick Reference

Fall 2017 December 4, 2017

Scheme Quick Reference

CSc 520 Principles of Programming Languages

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

a graph is a data structure made up of nodes in graph theory the links are normally called edges

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Structure and Interpretation of Computer Programs

Administrivia. Simple data types

Implementing Programming Languages

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CS61A Notes Disc 11: Streams Streaming Along

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Structure and Interpretation of Computer Programs

CS F-11 B-Trees 1

6.001 Notes: Section 15.1

Programming II (CS300)

Spring 2018 Mentoring 8: March 14, Binary Trees

Data Structures and Algorithms

Transcription:

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20 Name: Login: cs3- First names of the people to your left and right, if any: Left: Right: 1. If you have difficulty understanding exactly what a question is asking, please ask us for clarification as soon as possible. 2. Assume that your procedures will only be given inputs of the form described in the problem statement. You don t need to anticipate / check for inputs with the wrong number / type of arguments, etc. You must explicitly define any procedures that you use that aren t built into STk, even if you ve previously defined them in lab. 3. On problems in which you fill in blanks in our code, you may not add to or change the existing code, and what you fill into the blanks should be atoms or (at most) very short compound expressions. You will probably get less credit or no credit if you try to cram in much longer answers, on the grounds that your solution is unnecessarily complex. 4. There are no restrictions on what you may use to solve coding problems, except that you may not use set!, set-car!, and set-cdr!, since we never studied them. (They would probably not be particularly helpful anyway on this exam.) # Question name Possible Your score 1 What Will Scheme Print? 9 2 Miscellany 17 3 Half Off 9 4 Fourbearance 8 5 Film No-R 8 6 Treetotaling 8 7 Telegraph Troubles 16 TOTAL 75

Question 1: What Will Scheme Print? Suppose that you type each of the following into STk. What will STk print in each case? (Treat the problems as independent, with STk starting in its default state each time.) If STk would print something like #[closure arglist=...], just write procedure. If you would get a crash / error message (for any reason), just write error, unless the error would be a segfault or infinite hang due to running forever, in which case just write run-forever. STk> (keep (not odd?) '(1 23 456)) STk> (cddr '(dance dance revolution)) STk> ((lambda (x) (x)) (lambda () 'y)) STk> (append (list 1 '()) (cons 2 '())) STk> (accumulate append '() '((one) ((two)))) STk> (vector-set! (vector-set! #(0 1) 0 1) 1 0) STk> (define (z) (z)) z STk> (and (or #t (z)) (not (z))) STk> (define (peculiar a) (let ((b a)) b) b) peculiar STk> (peculiar 'hmm) STk> (define (new-even? n) (not (new-odd? n))) new-even? STk> (define (new-odd? n) (not (new-even? n))) new-odd? STk> (new-odd? 2011)

Question 2: Miscellany I. (4 pts) Answers are either true or false for very good (not incidental, obscure, or sneaky) reasons. True False 1. Each of the four built-in sentence HOFs (every, keep, accumulate, repeated) takes a procedure of one argument as its first argument. True False 2. A list can be defined recursively as a pair in which the cdr is either the empty list or another list (as per this same definition). True False 3. A vector and all of its entries (e.g., numbers, Booleans, other vectors) are guaranteed to all reside together in the same contiguous block of memory, with nothing else in between. True False 4. The amount of running time needed to find the number of occurrences of a particular value in a vector (e.g., all the 2s in '#(2 two #t 2 (2)) has (little to) no dependence on the length of that vector. II. (2 pts) Circle any/all of the following that are examples of selectors for the indicated data types. filter for sentences item for words leaf? for Trees vector-set! for vectors III. (4 pts) For each of questions 1-4, choose a single answer from among the following. A given answer may be used once, more than once, or not at all. A. car B. 'car C. (car) D. '(car) E. none of A-D F. more than one of A-D 1. Typing which of these at the STk prompt is equivalent to typing (quote car)? 2. Which of these, if typed in at the STk prompt, would return a list of length 1? 3. Which of these would cause an error if typed in at the STk prompt? 4. Which of these, if typed in at the STk prompt, would cause Scheme to print: car

Question 2, continued IV. (4 pts) Here is a good implementation of append: (define (new-append ls1 ls2) (if (null? ls1) ls2 (cons (car ls1) (new-append (cdr ls1) ls2)))) Assume that the running time of append is directly linearly proportional to the number of calls to cons, car and cdr. (Also assume that cons, car, and cdr take the same amount of time for any inputs.) Circle one of the three options from each bolded group below. Doubling ls1 s length would (not significantly change / roughly double / roughly quadruple) the running time of new-append. Doubling ls2 s length would (not significantly change / roughly double / roughly quadruple) the running time of new-append. V. (3 pts) You have a perfectly balanced / triangular-looking binary search tree (BST) called pyramid, in which every branch node has either two branch nodes or two leaf nodes as children, and leaf nodes appear only at the deepest depth of the tree. pyramid has one node at depth 0 (the root), two nodes at depth 1, four nodes at depth 2, eight nodes at depth 3, and sixteen (leaf) nodes at depth 4. pyramid contains only numbers as data, and no numbers are duplicated. Fill in the blanks in the expression below so that it will return the largest number in pyramid. You should not need to remember exactly how we implemented binary trees as lists; use datum, left-child, right-child, leaf?, make-binary-tree, etc. instead of their car, etc. equivalents. ( ((repeated ) pyramid))

Question 3: Half Off Suppose that you re trying to implement a procedure left-half, which is supposed to take a word and return its left half (i.e. the first n/2 characters, if the word is n characters long; you may assume that n will always be even.) But the procedure isn t working! (define (left-half word) (let ((halfsize (/ (length word) 2))) (define (helper rest) (if (< (length rest) halfsize) '() (word (first rest) (left-half (bf rest))))) (helper word))) 1. You type (trace left-half) into STk, but the trace output isn t very useful. Then you type (trace helper) into STk, but you get an error message. Why? Choose one. A. helper has been defined as a special form, and special forms can t be traced. B. In Scheme, there is no way to trace procedures defined inside of other procedures. C. No binding for helper exists in the global environment. D. Only one procedure may be traced at a time; you must (untrace left-half) first. E. traces within local environments still happen, but the output is not displayed to the user. 2. Fix all bugs in the above procedure so that it will work properly. You may only fix a bug by crossing out a small part of the code and replacing it with something else. Do not change large segments of the code or add in new lines. There are no bugs pertaining to parentheses. 3. Does the recursive procedure left-half generate a recursive or iterative process?

Question 4: Fourbearance In some Asian cultures, the number 4 is unlucky. Write a procedure (no4 proc) that takes a procedure proc (that takes one argument) and returns a new procedure that has the exact same behavior as proc, except that when it s given an input for which proc would normally return the number 4, the number 8 is returned instead. (proc will not necessarily always return a number.) You should not replace any other possible return values like -4, 44, 4chan, four, (4), or iv... just the number 4 itself. Moreover, your implementation must not call proc more than once -- what if proc is random, or involves mutation, for example? Examples: STk> (no4 abs) [#closure arglist...] STk> (define new-square (no4 square)) new-square STk> (new-square 2) STk> ((no4 (lambda (x) 4)) 6) 8 8 STk> (new-square 4) 16 STk> ((no4 word?) 'four) #t 2. Assuming that we ve already typed (define (square x) (* x x)), how many different values could (square ((no4 random) 10)) possibly return if you typed it in arbitrarily many times? (You don t need to enumerate these possibilities... just say how many possibilities there are.)

Question 5: Film No-R Ian worked in a movie theater once, and had to pick out the big plastic letters for the marquee (the movie title display on the outside of the theater). Sometimes the most common letters ran out. film-no-r is a pun on film noir, in case you were wondering... Write a procedure film-no-r that takes a list of movie titles (each of which is a sentence) and returns the same list, but with every instance of the letter r removed from every word. STk> (film-no-r '((friends with benefits) (x-men first class) (harry potter))) ((fiends with benefits) (x-men fist class) (hay potte))

Question 6: Treetotaling Write a procedure (best-total t) that takes a Tree t in which every datum is a number, and returns the maximum value attainable from adding up all of the numbers in some subtree of t. Any node (branch or leaf) of t, including the root node, counts as a subtree; a subtree is a node and all of that node s descendants. (So for a Tree with n nodes, there are n subtrees that could have the best total.) Use datum, children, leaf?, etc. instead of car, cadr, etc.; you may use these without defining them, and you shouldn t need to remember exactly how we implemented Trees. You may assume that the Tree will contain at least one node. 4 For example, for the Tree to the left, / \ best-total should return 5. (This is the result -2 3 of adding up all of the values in the bolded / \ subtree. All of the other subtrees produce 3 4 0-8 smaller totals: 4, -5, 3, 4, 0, -8. You must add all of the numbers in some particular subtree; you couldn t take, e.g, just the 4 and the 3 below it, or the 4 and the -2 and 3 immediately below it, because neither of those would form a full subtree (they would omit some descendants).

Question 7: Telegraph Troubles In Morse code, each letter in the English alphabet is represented by a series of short sounds (written as dots, but we ll use asterisks because of the special meaning of dots in Scheme) and/or long sounds (written as dashes). Here are all of the English letters that can be represented with at most two dots and/or dashes. In these problems, we will only deal with these six letters. A E I M N T *- * ** -- -* - 1. (2 points) Suppose that the association list morse is already defined: (define morse '((* e) (- t) (** i) (*- a) (-* n) (-- m))) Fill in the blanks to complete the procedure (from-morse ml), which takes a word ml (a single Morse letter, which will always be either *, -, **, *-, -*, or --), and returns the corresponding English letter (E, T, I, A, N, or M, respectively). For example, (frommorse '-*) should return n. (define (from-morse ml) ( ( ml morse))) To transmit a Morse word, you transmit each Morse letter, leaving a pause (written as a space) between each letter and the next. For example, the word TEATIME would normally be: - * *- - ** -- *. However, you re receiving messages from an inexperienced Morse code operator who hasn t realized that he needs to put spaces between the letters. For example, he would transmit TEATIME as: -**--**--*, and since you don t know where the spaces should have gone, this could also be reasonably interpreted as NANETTE: -* *- -* * - - * or a number of other words that may not actually be English words. 2. (10 points) Fill in the blanks to complete the memoized version of (parse-morse mw), which takes a word mw consisting only of dots (asterisks) and dashes and returns a sentence of all of the possible translations of mw into English letters, given that spaces could be anywhere in mw. (The order in which these translations are returned is unimportant.) Example: STk> (parse-morse '-*-*) (tete ten tae nte nn)

Question 7, continued Reminder: For fill-in-the-blank questions, do not alter the existing code outside of the blanks, and everything you fill into a blank should be rather short, or you're probably overcomplicating things (and may not get credit!) (define (parse-morse w) (let ((memovect (make-vector (+ (count w) 1) ))) (define (helper rest) (if ( rest) (let ((lookup (vector-ref memovect (count rest)))) (if lookup (let ((answer ( (every (lambda (x) (word (from-morse (first rest)) x)) (helper (bf rest))) (if (< (count rest) 2) (every (lambda (x) (word (from-morse (word (first rest) x)) )) (helper )))))) (vector-set! memovect ) answer))))) (helper w)))

Question 7, continued 3. (2 points) Our morse association list makes it convenient to go from a Morse letter to an English letter, but what if we want to be able to translate in the other direction? Write a procedure (ass-backwards al) that takes an association list al (any list, not necessarily Morse-related) and returns a new association list in which every two-item sublist has been flipped: STk> (ass-backwards morse) ((e *) (t -) (i **) (a *-) (n -*) (m --)) (define (ass-backwards al) ( (lambda (sublist) ( (cadr sublist) (car sublist))) al)) 4. (2 points) Give a mathematical expression/description for the number of items n in the list returned by (parse-morse mw) when the number of characters in mw is c. Your answer should depend on c. Make sure your expression/description is specific enough. (Hint: this is very similar to at least two problems that you ve seen before in lab...) n =