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

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

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

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

SCHEME AND CALCULATOR 5b

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 3L (Clancy) Solutions and grading standards for exam 1

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

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

CS3 Fall 05 Midterm 1 Standards and Solutions

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

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

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

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

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

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

Solutions and grading standards

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


YOUR NAME PLEASE: *** SOLUTIONS ***

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

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

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

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

CS3 Midterm 1 Fall 2007 Standards and solutions

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

CS3 Midterm 1 Fall 2006

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

Functions that return lists

CS3 Midterm 2 Standards and Solutions

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

CS3 Midterm 2 Summer 2008

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Drawing Hands, by M. C. Escher (lithograph, 1948)

6.001 Notes: Section 8.1

CSE341 Spring 2017, Final Examination June 8, 2017

CS61A Midterm 2 Review (v1.1)

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

We learned in Chapter 20 how to read from the keyboard and write to the screen. The

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

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

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

Excel Basics: Working with Spreadsheets

University of California, Berkeley College of Engineering

This should prevent residual sexism, racism, favoritism lurking in the unconscious of your professor & TA from biasing grading!!

2010Fa CS10 Online Final Answers Section 1

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

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

What s the base case?

Discussion 4. Data Abstraction and Sequences

University of Massachusetts Lowell

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

Computer Science Foundation Exam

Building a system for symbolic differentiation

CS150 - Sample Final

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

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

CS 3 Midterm 1 Review

CSE341 Spring 2017, Final Examination June 8, 2017

User-defined Functions. Conditional Expressions in Scheme

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

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

by the evening of Tuesday, Feb 6

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

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

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CS150 Sample Final. Name: Section: A / B

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

CS3: Introduction to Symbolic Programming. Lecture 5:

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Repetition Through Recursion

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

ITERATORS AND STREAMS 9

Building a system for symbolic differentiation

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Read and fill in this page now

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Midterm 2 Solutions. CS70 Discrete Mathematics and Probability Theory, Spring 2009

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

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

CS450 - Structure of Higher Level Languages

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

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Performance Assessment Spring 2001

CS150 Sample Final Solution

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

EXAMS IN THE GENESIS GRADEBOOK

Part II Composition of Functions

Trombone players produce different pitches partly by varying the length of a tube.

Comp 311: Sample Midterm Examination

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Transcription:

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon) Name: Login: cs3- First names of the people to your left and right, if any: Left: Right: 1. You may solve coding questions using any combination of recursion and built-in HOFs. You may use helpers. 2. If you have difficulty understanding the language of a question, or exactly what the question is asking, please ask us for clarification as soon as possible. 3. 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. 4. You must explicitly define any procedures that you use that aren t built into STk, even if you ve previously defined them in lab. # Question name Possible Your score 1 Substitute Substitutes 8 2 Two Ways To Fib 8 3 IM IN UR SET 10 4 I Think I STked Last Night 7 5 Poll Positions 8 6 Tick Tock 8 7 Making the Grades 13 8 Do As The Bromans Did 18 TOTAL 80

Question 1: Substitute Substitutes In lab, you wrote the substitute function, which replaces any and all instances of a word from in a sentence sent with a new word to: (define (substitute sent from to) (cond ((empty? sent) '()) ((equal? (first sent) from) (se to (substitute (bf sent) from to))) (else (se (first sent) (substitute (bf sent) from to))))) > (substitute '(that moon is no moon) 'moon 'man) (that man is no man) The above procedure never crashes, and always produces the correct result for any inputs. Quibble Nitpickerson hates typing out (substitute (bf sent) from to) in two different places in the program, and he has proposed two different ways to rewrite substitute to avoid this. Here s one of his attempts: (define (substitute2 sent from to) (if (empty? sent) '() (se (substitute2 (bf sent) from to) (if (equal? (first sent) from) to (first sent))))) Which of the following best characterizes substitute2? Circle only one. A. It will never crash, and it will always produce the correct result. B. It will never crash, but its result will be correct in some cases and incorrect in other cases. C. It will never crash, but its result will always be incorrect. D. It may crash, produce incorrect results, or produce correct results, depending on the inputs. E. It will always run forever (or at least until it segfaults / coredumps). F. It will always cause a bad function error. G. It will always cause an invalid argument error. H. It will always cause an unbound variable error.

Question 1, continued Here s another of Nitpickerson s attempts, in which he uses a let to avoid the redundancy: (define (substitute3 sent from to) (let ((nextstep (substitute3 (bf sent) from to))) (cond ((empty? sent) '()) ((equal? (first sent) from) (se to nextstep)) (else (se (first sent) nextstep))))) Which of the following best characterizes substitute3? Circle only one. A. It will never crash, and it will always produce the correct result. B. It will never crash, but its result will be correct in some cases and incorrect in other cases. C. It will never crash, but its result will always be incorrect. D. It may crash, produce incorrect results, or produce correct results, depending on the inputs. E. It will always run forever (or at least until it segfaults / coredumps). F. It will always cause a bad function error. G. It will always cause an invalid argument error. H. It will always cause an unbound variable error.

Question 2: Two Ways To Fib Let F(n) denote the nth Fibonacci number. F(1) is 0, F(2) is 1, and for n > 2, F(n) = F(n-1) + F(n-2). So F(3) = 1, F(4) = 2, F(5) = 3, F(6) = 5, and so on. In lab, you ve seen the following procedure for generating the nth Fibonacci number: (define (fib n) (if (<= n 2) (- n 1) ; i.e., 0 for n = 1, 1 for n = 2 (+ (fib (- n 1)) (fib (- n 2))))) 1. Suppose that you type in (fib 5). Including this original call, how many total calls to fib will be made? 2. Fill in the blanks to complete the following alternate procedure for generating the nth Fibonacci number. It starts off knowing F(1) and F(2), generates each new Fibonacci number by adding together the two it currently knows, and remembers which Fibonacci number it s currently on. Do not otherwise add to or alter the existing code. (define (fib2 n) (define (fib2helper smaller larger currentnum) (if (= currentnum ) (fib2helper larger (fib2helper 0 1 1)) ))) 3. Both fib and fib2helper are recursive procedures, but which one(s), if any, generate a recursive process? Circle any and all that apply: fib fib2helper

Question 3: IM IN UR SET The following procedure, (is-subset? s1 s2), takes two sentences s1 and s2 and returns #t if every item in s1 is also in s2, and #f otherwise. Each sentence contains only numbers, in sorted order (from smallest to largest), and no number is duplicated within a sentence. Here are some examples of the intended behavior: STk> (is-subset? '(2 4) '(1 2 3 4 5)) STk> (is-subset? '(2 5) '(2 3 4)) #t #f STk> (is-subset? '() '(1 2 3 4)) STk> (is-subset? '(1) '()) #t #f STk> (is-subset? '() '()) STk> (is-subset? '(1 2 3) '(1 2 3)) #t #t 1. Fill in the blanks to complete is-subset?. Do not otherwise add to / alter the existing code. (define (is-subset? s1 s2) (cond ((empty? s1) ) ; clause I ((empty? s2) ) ; clause II ((< (first s1) (first s2)) ; clause III ) ((> (first s1) (first s2)) ; clause IV (else ) ))) 2. The cond clauses are currently in the order I, II, III, IV. Which of the following orderings for the first four cond clauses in is-subset? would create the potential for an error (crash) or a wrong answer? (A single cond clause includes both the test and the outcome of the test, not just the test.) Circle all that apply. I, II, IV, III I, III, II, IV II, I, III, IV

Question 4: I Think I STked Last Night You wake up after a party to find that you apparently typed the following into STk. Must have been a wild night! STk> (define honeyimsorry (list 1 (append (cons '(2) '()) (cons '() (list 3))))) honeyimsorry 1. What does Scheme print when you type honeyimsorry at the prompt? (The expression does not cause an error, and the output does not have any dots. in it -- only numbers and parentheses.) The following space is for any box and pointer diagrams that you choose to draw. You will not be graded on what you draw here. You can draw an adorable farm animal if you so desire. 2. Fill in combinations of car and cdr (represented as shorthand like caddar) that would make STk print/return 1, 2, and 3, respectively. (Be careful; returning (2) isn t the same as returning 2, for example.) You will get credit as long as these are either correct or consistent with a wrong answer to part 1. Although STk only accepts up to four as/ds between the c and r, you can put in as many as you want, but you should only be filling in as and ds. (c r honeyimsorry) returns 1 (c r honeyimsorry) returns 2 (c r honeyimsorry) returns 3

Question 5: Poll Positions A certain polling place has a (positive) number of voting booths lined up in a single row. Voters are currently using some of them. However, any voter who is directly next to another voter (on either side or on both sides) feels nervous. (The voting booths on either end of the row are next to walls; walls do not make voters feel nervous.) Any voter who is not nervous is calm. Write (num-calm layout), which takes a word layout (of positive length) consisting only of vs (which represent voters using voting booths) and/or es (which represent empty voting booths), and returns the number of calm voters. Examples: STk> (num-calm 'v) STk> (num-calm 'evve) 1 0 STk> (num-calm 'vev) STk> (num-calm 'evvvevev) 2 2

Question 6: Tick Tock Ke$ha has just finished partying at the Telephone Nightclub, and she s successfully gotten a little bit tipsy. Luckily, it s a linear (one-dimensional) walk home from the club to her house. The club is at position 0, Ke$ha starts at position 1, and her house is at position h. (h is 2 or greater.) Ke$ha can make one move per minute, in either direction (and must move; she can t just stand still), but she only has t minutes (and therefore t moves) before she passes out. If Ke$ha ever moves to position 0, where the club is, she immediately resumes partying (and makes no more moves), and therefore never gets home. If she ever moves to position h, she is successfully home (and makes no more moves). Even if she reaches home just as she runs out of time, it still counts as getting home. If she runs out of time and she is anywhere other than home, she obviously never gets home! Write (ways-home t h), which returns the number of distinct ways in which Ke$ha can get home in t minutes or less, given that her house is at position h. Here s an example in which h = 3. (Remember that Ke$ha always starts at 1, and the club is always at 0.) (ways-home 0 3)and(ways-home 1 3) should both return 0, since there s no way to get home in time. (ways-home 2 3) should return 1, since the only way to get home in time is to make two moves to the right. Even though she reaches home just as time runs out, it still counts as getting home. (ways-home 3 3) should return 1, since Ke$ha can make it home with two moves to the right, as in the previous solution, but there is no other series of three moves or less that makes it home. (ways-home 4 3) should return 2; the possibilities are two moves to the right, or four moves: right, left, right, right.

Space to answer Question 6

Question 7: Making the Grades Imagine that Ian is storing the grades for CS3L in a big list called a gradebook. A gradebook is a list of student s. A student is a two-item list: * The first item is a word consisting of the last two letters of the student s login. * The second item is a list of that student s scores on assignments. Moreover, there is one fictional student, pp, whose list of scores represents the point values of the assignments. You can think of pp as standing for Points Possible. You may assume that any gradebook will always have at least one student besides pp and at least one assignment, all assignment values are positive, and every student has a score for every assignment. No logins will be duplicated, and students are stored in the gradebook in alphabetical order by login. For example, here s a simple gradebook that contains only two students, with logins da and db, plus pp, and data for two assignments worth 5 and 3 points, respectively. Student da got 4/5 on the first assignment and 2/3 on the second assignment. (define my-grades '((da (4 2)) (db (2 2)) (pp (5 3)))) 1. Complete the following (glookup login gb) procedure, which takes a student s login and the name of a gradebook as an argument and returns that student s overall grade (all points earned divided by all points possible, as a percentage out of 100). (glookup-helper takes a login and computes the total of all the scores recorded for that login.) Using the example above, (glookup 'da my-grades) should return 75. Fill in each blank with a single atom (i.e. not a compound expression), and do not otherwise add to / alter the existing code. (define (glookup login gb) (define (glookup-helper who) ( ( ( who gb)))) (* (/ (glookup-helper login) (glookup-helper 'pp)) 100))

Question 7, continued 2. Write (add-assignment scores gb), which takes scores (which is a list of two-element lists of student logins and those students scores on the assignment, including pp s score, which is the assignment s point value) and a gradebook gb. add-assignment should return a new gradebook in which every student s score has been added to the end of that student s score data. The logins in scores are not necessarily in the same order as they are in gb. Moreover, not all logins in the gradebook will necessarily appear in scores; you should give any missing students a score of 0 for the assignment. However, you are guaranteed that all logins in scores are actually in the gb, and that no login has duplicate entries in scores. The returned gradebook must have the correct format for a gradebook, as described earlier. For example: (add-assignment '((db 8) (pp 10)) my-grades) would return: ((da (4 2 0)) (db (2 2 8)) (pp (5 3 10)))

Question 8: Do as the Bromans Did The little-known Broman civilization probably collapsed because of its number system. The Bromans used letters as digits, but they only had the letters B, O, and R. We know that B stood for 1, but we only know that O and R stood for different integer values o and r, both greater than 1. A Broman numeral s value in our number system is equal to the sum of the values of its digits, with the exception that the sequence BRO (consecutively, in that order) is worth some value bro, and isn t calculated as the sum of its parts 1 + r + o. So, if o = 5, r = 25, and bro = 100, then: OR = 30; ROB = 31; BORO = 36; BOOR = 36; BROO = 105; BROBROBRO = 300 1. Write (from-broman numeral o r bro), which takes a Broman numeral and returns its decimal value, given that the values of O, R, and the consecutive BRO are o, r, and bro. Examples: STk> (from-broman 'OR 33 66 100) STk> (from-broman 'OBROB 3 30 100) 99 104

Question 8, continued As you saw from the previous examples of BORO = 36 and BOOR = 36, the same number can be represented by more than one Broman numeral. But the Bromans had a preferred representation of each numeral: it was the possibility that was shortest, and, if one or more possibilities tied for shortest, it was the alphabetically earliest of the shortest possibilities. (Remember that (before? word1 word2) will return #t if word1 is alphabetically earlier than word2, and #f otherwise.) 2. Write (to-broman num o r bro), which takes a number num and returns the preferred Broman representation, given that the values of O, R, and the consecutive sequence BRO are o, r, and bro. Examples: STk> (to-broman 7 5 10 6) STk> (to-broman 12 3 6 10) BBO RR STk> (to-broman 10 3 6 10) STk> (to-broman 8 15 25 6) BOR BBBRO