CS61A_sp02_mt1

CS61A_sp02_mt1 - CS 61A Midterm#1 — Wed Februrary 20th...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 61A Midterm #1 — Wed, Februrary 20th, 2002 Your name login: csfila— Discussion section number Circle your Chris Chris David Donald Erwin Ryan Seema Todd Yaping TA’S name Cheng Karlof Schultz Chai Vedar Stejskal Moorjani Segal Li The student sitting to my right is The student sitting to my left is ____._—______.___ This exam is worth 50 points, or about 17% of your total course grade. It includes two parts: The individual exam (this part) is worth 44 points, and the group exam is worth 6 points. The individual exam contains six substantive questions, plus the following: Question 0 (1 point): Fill out this front page correctly and put your name and login correctly at the top of each of the following pages. This booklet contains six numbered pages including the cover page. Put all answers on these pages, please; don’t hand in stray pieces of paper. This is an open book exam. When writing procedures, don’t put in error checks. Assume that you will be given arguments of the correct type. Our expectation is that many of you will not complete one or two of these questions. If you find one question especially difficult, leave it for later; start with the ones you find easier. READ AND SIGN THIS: I certify that my answers to this exam are all my own work, and that I have not discussed the exam questions or answers with anyone prior to taking this exam. If I am taking this exam early, I certify that I shall not discuss the exam questions or answers with anyone until after the scheduled exam time. (a) Question 1 — I’m drawing a blank... (12 points): Fill in the simplest possible answer in the blank so that the expression produces the result. > ((lambda (x) (it x x)') ) usa Given the following two definitions, fill in zero or more parentheses only (P0) in each blank to complete the scheme expression and show the numeric return value on the right. (define (double 1) (* 2 x)) (define Olympics (lambda (x) (lambda (f) (f (f x))))) >+_ Olympics _ Olympics _ 5 __ double _ double __ => P0 P0 P0 P0 P0 P0 must be a number Fill in the result. If the expression produces an error, just say “error”; if it returns a procedure, just say “procedure”; if it causes an infinite loop, just say “loop”. > (let ((y ’(gold))) (cons (append y (list y)) y)) Fill in the result. If the expression produces an error, just say “error”; if it returns a procedure, just say “procedure”; if it causes an infinite loop, just say “loop”. > (define (bronze 11) (define (iter count 1) (if (<= 1 1) count (iter (+ count 1) (- i 2)”) (iter 0 n)) > (bronze 5000) Fill in the result. If the expression produces an error, just say “error”; if it returns a procedure, just say “procedure”; if it causes an infinite loop, just say “100D”, You may assume pigl is defined exactly as it is in the notes. > (every pigl (se ’0 ’class ’(beats) (word 61 ’a))) Your name login csfila— Question 2 — Eve Ensler was discussing “Big-O” notation... (3 points): This question concerns the following functions. (define (f x) (define (g y) (define (h 2) (if (< x 0) (if (< y 100) (if (< z 0) 1 0 0 (f (- x 2)),)) (+ (f y) (+ (h (- z 2)) (f (- y 1))))) (h (- z 1))))) Circle (T)rue or (F)alse: T F h generates an iterative process (i.e., uses 8(1) space). T F g generates an iterative process (i.e., uses 8(1) space). T F f is @(x). T F h is @(zz). T F f and g have the same order of growth. T F g and h have the same order of growth. Question 3 —— Waiter, please take my order of evaluation... (4 points): This question concerns the following functions. (define (square 1) (* x 1)) (define (foo x y) (* x y y)) (define (bar I y) x) (a) How many times is * called for (foo (square (* 1 1)) (square (* 1 1))) . . .under normal order? . . . under applicative order? (b) How many times is * called for (bar (square (wk 1 1)) (square (* 1 1))) . . . under normal order? . . .under applicative order? Question 4 — If you can’t be an athlete, be an athletic supporter... (8 points): We designed an abstract data type, athlete, to repmsent—Qlizmpic athletes. An athlete contains 3 pieces of information: name (amdfi: country (a word}; and events (a sentence containing the names of all events the athelete partiéipates-in); 'Here is‘the constructor: ' (define (make-athlete name country events) (append (1181? name country) (cons events ’()))) (a) Write the appropriate selectors for the athlete ADT. (b) We have created a list of athletes as follows: (define all-athletes (list (make-athlete ’Picabo-Street ’USA (se ’slalom ’downhill)) (make-athlete ’Todd ’USA (se ’slalom ’super-monkey-ba11)) (make-athlete ’Johnny-Hoseley ’USA (se ’moguls ’video—games)) more athletes )) e wr t—participants that takes an event and )fist‘of athletes as input and returns a sentence Containing the names of all athletes who participate in the events. E.g., > (get-participants ’slalom all-athletes) => (Picabo—Street Todd) Complete the definition of get—participants. Respect the Data Abstraction! (define (get—participants event list-of—athletes) (cond (( list—of-athletes) ’0) ((member? event- (n___.__ (~__—__ list—of-athleteSD) L— (*____ (h list-of-athletes)) (get-participants event (__..__ list-of-athletes)))) (else (get-participants event (____ 1ist-of-athletes)))) ) 4 Your name login csfila— Question 5 — I can’t talk right now, my head is in a vice... (8 points): This question concerns the twenty-one project (the version without jokers). The CS61A staff is going to Las Vegas, and we plan to play a lot of twenty-one! One of the questions in the project asked you to create your own strategy. We would like to figure out which of these strategies is the best, so we can use it in Vegas and win billions. The procedure bestjakes an integer n and a non-nulllist of strategies as arguments. best will play twenty-one with each strategy 11 times and return the strategy that had the best results—in the case of a tie, any winner may be returned. Fill in the blanks to complete the definition of best. Remember, you can assume that the list of strategies is not empty. Do not create any new procedures! (define (best 11 list-of-strats) (define (helper best-strat best-result strats-left) (if ( ) (let ((result )) (if (< __._r____._.______.____. H____.______.) W» )) (helper ) ) ARE YOU SURE THAT‘S RANDOM? THAT'S THE PROBLEM MITH RAN— DOMNESS' YOU CAN , NINE NINE OVER HERE NINE NINE LUE HAVE OUR *: NINE NINE RANDOM NUMBER : GENERATOR. NEVER BE fili‘lm n “:01 Imam! rent-am fiyndlcple Inc Copgright Q 2881 United Feature Sgndicate, Inc. (a) Question 6 —- Dial 1—800—ca11-a11 and save a buck or two... (8 points): Louis Reasoner is trying to write a function call-all that takes a list of single—argument functions and returns a new unary function. The function returned by callaall should call each of the function in the list with its input and return a list of the results. E.g., > (define (identity 1) x) > (define (square 1) (a: x x)) > (define (cube 1) (* x x 1)) > (define powers (call-all (list identity square cube») > (powers 2) (2 4 8) > (define pigl-and-reverse (call-all (list pigl reverse))) > (pigl-and-reverse ’class) (assclay ssalc) > (define no-results (call-all ’())) > (no-results 100) () Louis’ first attempt at writing call-all looks like: (define (call—all L) (if (null? L) 1 ’ () 2 (lambda (x) 3 (cons ((car L) x) 4 (call—all (cdr L)))) )) Complete the sentence below to describe what Louis’ program returns versus what it is supposed to return (according to the specifications). If the expression produces an error, just say “error”; if it returns a procedure, just say “procedure”; if it causes an infinite loop, just say “loop”. “A call to ((call—all (list square 1+)) 2) returns but should return . .” Make as few changes as necessary to fix the procedure so that it works as advertised on all inputs. You will probably not need to change all the lines. Change line to Change line to Change line to Change line to __. _ _____.________.______._._.—————————— ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern