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

Similar documents
Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

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

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

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

CS 61A Midterm #2 - October 5, 1998

Discussion 4. Data Abstraction and Sequences

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

User-defined Functions. Conditional Expressions in Scheme

University of Massachusetts Lowell

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

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

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

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

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

YOUR NAME PLEASE: *** SOLUTIONS ***

The compiler is spewing error messages.

CSE413 Midterm. Question Max Points Total 100

University of California, Berkeley College of Engineering

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


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

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

PROBLEM SOLVING 11. July 24, 2012

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

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

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Discussion 11. Streams

CS450 - Structure of Higher Level Languages

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

Repetition Through Recursion

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

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

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

Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

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

Structure and Interpretation of Computer Programs

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

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

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Structure and Interpretation of Computer Programs

Using a percent or a letter grade allows us a very easy way to analyze our performance. Not a big deal, just something we do regularly.

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

Structure and Interpretation of Computer Programs

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

(Refer Slide Time: 01.26)

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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

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

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

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

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Midterm Practice Exam Sample Solutions

Structure and Interpretation of Computer Programs

Sample Final Exam Questions

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

CSE341 Spring 2017, Final Examination June 8, 2017

Problem 1: Binary Trees: Flatten

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

CS61A Midterm 2 Review (v1.1)

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

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

Structure and Interpretation of Computer Programs

CS61A Notes Disc 11: Streams Streaming Along

Learn Ninja-Like Spreadsheet Skills with LESSON 9. Math, Step by Step

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

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

CS 2316 Exam 4 Fall 2011

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

CS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012

Functional Programming. Pure Functional Languages

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

Building a system for symbolic differentiation

Functional programming with Common Lisp

This exam has 10 pages including the title page. Please check to make sure all pages are included.

CS 051 Homework Laboratory #2

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

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

CS 415 Midterm Exam Fall 2003

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

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

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

CS184, Fall 1997 Midterm #1 Professor Brian A. Barksy

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

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

Chapter 7. Polygons, Circles, Stars and Stuff

CS 415 Midterm Exam Spring SOLUTION

Searching Algorithms/Time Analysis

UNIVERSITY REGULATIONS

Transcription:

University of California, Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia 2000-12-15 CS 3 Final Exam Last name First name SID Number TAs name (Sorry to ask this next question, but with almost 375 students, there may be a wide range of behavior.) The student on my left is The student on my right is I certify that my answers are all my own work. I certify that I shall not discuss the exam questions or answers with anyone in CS3 who has yet to take it until after the scheduled exam time. Signature Instructions Please write your name on every page! The exam is open book and open notes (no computers). Put all answers on these pages; don t hand in any stray pieces of paper. You may always write auxiliary functions for a problem unless they are specifically prohibited in the question. Feel free to use any Scheme function that was described in sections of the textbook we have read without defining it yourself. You do not need to write comments for functions you write unless you think the grader will not understand what you are trying to do otherwise. Do every question, as we will not be dropping your lowest-scoring one. You have three hours, so relax. We have indicated how long you should take for each question at the top of every page. Good skill! Grading Results Question Max. Points 1 20 2 20 3 20 4 20 5 20 6 20 7 20 Total 140 Points Given

Question 1 Iʼm drawing a blank (20 points, 2 points each; 20 minutes) Fill in the blanks below. When you see the symbol, this means you should write down what the interpreter would return if the expression were typed in. If any of the following display an error, write down what the error is. a) (cdr (cons () ()) ). b) (map reverse ((a) (b) (c)) ). c) (map (4 5 6) 3). d) (equal? (quote (1)) (list (1)) ). ;; For parts e-g (define (mystery L) (cond ((null? L) #t) ((list? (car L)) #f) (else (mystery (cdr L))))) e) mystery is a type of recursion. f) (mystery (a (b) c)). g) A good name for mystery would be. ;; For parts h-i ;; wackawacka is not defined anywhere (define (broken L) (wackawacka L)) h) Does entering the above definition into scheme result in an error?. i) (if f (broken (/ 1 0)) 3). j) The Turing test is a test of. Page 2 of 9

Question 2 Ready, set, code! (20 points; 25 minutes) Assume for this problem that sets and lists only contain numbers. You want to write a function pretty-print that prints either a set or a linear list to the user: (define (pretty-print set-or-list) (cond ((set? set-or-list) (display "Your set is: ") (display (set-contents set-or-list))) (else (display "Your list is: ") (display set-or-list)))) (define (set-contents set) set) The problem is how do we write the set? predicate? Lists and sets look the same! Therefore, we are going to create a new representation for sets (called a new-set). While we are doing that, we might as well keep track of the set s cardinality, or number of elements in the set. The new representation will be a three-element list, whose first element is the symbol set, the second element is the cardinality, & the third element is the set s contents. I.e., new-set = (set cardinality contents). Thus, a set containing the numbers 1, 4, 9, 16, and 25 could be represented as (set 5 (16 4 9 25 1)). *null-new-set* is defined as (set 0 ()). We also need to redefine set-contents as follows:(define (set-contents new-set) (third new-set)) We need to write set?, and realize that since lists can only contain numbers, all we have to do is to test is if the first element is the symbol set. Our first attempt is the following: (define (set? new-set-or-list) (= (first new-set-or-list) set)) a) Notice that set? has at least one error. Write it correctly below. (6 points) (define (set? new-set-or-list) ;; Returns #t for new-sets and #f for lists b) Since the format of sets have changed, we now need to rewrite new-set-adjoin. Feel free to use any of the new-set- functions on this page. (14 points) ;; Here are some more new-set functions (define (new-set-null? new-set) (equal? new-set *null-new-set*)) (define (new-set-member elt new-set) (member elt (set-contents new-set))) (define (new-set-length new-set) (second new-set)) (define (new-set-adjoin elt new-set) Page 3 of 9

Question 3 set in treeʼs clothing (20 points; 20 minutes) Now something else has cropped up. Recall the general definitions for trees: (define (root tree) (first tree)) (define (left tree) (second tree)) (define (right tree) (third tree)) (define (leaf? tree) (atom? tree)) (define (make-tree root left right) (list root left right)) A tree is an atom or a list with three items in it, the root and the two subtrees. We saw expression trees and value trees, which were two particular examples of trees, but not necessarily all the valid trees that exist. Since new-sets are now lists of three items, they could possibly be confused with trees. a) For this part, assume that a new-set can contain only positive integers (I.e., 1, 2, 3, etc.). Write an expression using only new-set-adjoin and *null-new-set* that returns a new set which is also a valid tree. Use values that insure that it will have the smallest sum of its set elements. Warning: be sure to re-read your answer several times to make sure it s correct. (10 points) b) Draw the corresponding tree. (10 points) Page 4 of 9

Question 4 Dick Trace-me (20 points; 25 minutes) Someone writes the strange program foo: (define (foo n) (cond ((= n 0) 'done) (else (display (list 'l n)) (foo (- n 1)) (display (list 'm n)) (foo (- n 1)) (display (list 'r n)) (newline) 'this-was-hard))) ;; left ;; middle ;; right a) A call to (foo 1) displays a line & returns a value as shown below (we left the return value blank). Fill in the blank with the return value. (4 points) : (foo 1) (l 1)(m 1)(r 1) b) What is displayed and returned when you call (foo 2)? (12 points) : (foo 2) Note above that (foo 1) displayed 3 pairs: (l 1), (m 1) and (r 1). c) How many pairs will be displayed for (foo 3)? (4 points) Page 5 of 9

Put down your pen or pencil, stretch, take a deep breath, and proceed If you followed our suggested pace, you should have 90 min left and be half done. Page 6 of 9

Question 5 Did you accumulate CS3 knowledge? (20 points; 30 min.) For parts (a) & (b), you may not need all the blank lines provided, and that s ok. a) Provide a call to the HOF all? (that returns true if all of the elements in the second argument satisfy the first argument predicate function) which will return #t if all of the numbers in the huge list of numbers *big-number-list* are greater than 3. E.g., if *big-number-list* contained (7 9 5), your call should return #t. Do not use any auxiliary function. (5 points) (all? *big-number-list*) b) You have seen how powerful accumulate is; it can be used to sort or find the smallest element in a list. You believe it is underutilized, and want to prove that (like a Swiss-army knife) there are many more uses for it. Fill in the blanks in my-count-if which, like count-if, will count the number of elements in the linear list L that satisfy pred?. You may assume L has at least two elements. Do not call any auxiliary functions. We consider this a hard question; you are more than welcome to skip this part for now and come back to it later in the exam. (15 points) (define (my-count-if pred? L) (accumulate (cons 0 L) )) Page 7 of 9

Question 6 Listen to what the flower people say (20 points; 30 min.) You come up with an idea for a cool fractal flower. The idea is to start with a stem (as in Figure 1) which is a line from the bottom center of the window to the middle. Then make a left and right turn and recurse. Figure 3 shows what happens when you do this a couple of times. We ve provided draw-half-line: ;; Draw a line from (x1,y1) halfway to (x2,y2) as in Figure 4 (define (draw-half-line x1 y1 x2 y2) (position-pen x1 y1) (draw-line-to (/ (+ x1 x2) 2) (/ (+ y1 y2) 2))) (x2,y2) (xm,ym) (xl,yl) (xr,yr) (x1,y1) n = 0 n = 1 n =? Figure 1 Figure 2 Figure 3 Figure 4 a) Fill in the blanks to complete the flower procedure below. Use Figure 4 to help you understand the temporary variables xm, ym, xl, yl, xr and yr. (15 points) (define (flower x1 y1 x2 y2 n) (if (= n 0) (draw-half-line x1 y1 x2 y2) (let ((xm (/ (+ x1 x2) 2)) (ym (/ (+ y1 y2) 2)) (xl (/ (- (+ x2 x1 y1) y2) 2)) ;; Do NOT worry about how we (yl (/ (- (+ x2 y1 y2) x1) 2)) ;; calculated xl,yl,xr or yr (xr (/ (- (+ x2 x1 y2) y1) 2)) ;; (yr (/ (- (+ x1 y1 y2) x2) 2))) ;; Simply look at Figure 4 ))) b) What was the value of n that generated Figure 3? (3 points) c) Modify Figure 3 to show the result of the next generation of flower (i.e., with n one larger than the correct answer to part (b) above). (2 points) Page 8 of 9

Question 7 Getting back at mom (20 points; 30 minutes) Computer scientists (and most of our moms) always seem interested in sorting things (like lists) and returning them to their proper place. You want to rebel and unsort things (like throwing all your clothes on the floor). Someone suggests writing unsort, a procedure which takes a list of unique elements and returns the same list with all of its elements rearranged in a random order. E.g., : (unsort (a b c d)) (b d c a) : (unsort (a b c d)) (d b a c) a) Fill in the blanks to complete the unsort procedure below. You may not use any auxiliary functions, & your solution may not exceed two lines. (10 points) (define (unsort L) (if (null? L) L ;; If L is null, just return it )) b) Your friend from Stanford types the following into your scheme interpreter: : (define let stanford-is-great) : (define let* we-won-the-big-game) which has the effect of redefining (and rendering unusable) let and let*. You are now asked rewrite unsort given that you cannot use these special forms. Fill in the blanks to complete the unsort procedure below. The solution does not require any auxiliary functions. However, if you do choose to use an auxiliary function, you will lose 5 points. Do not use set! or a define within a define. We consider this the hardest question on the exam. (10 points) (define (unsort L) (if (null? L) L ;; If L is null, just return it )) ;; If you need a small auxiliary function, write it below. You re done!!! Have a great winter break! Page 9 of 9