{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp03_mt2

CS61A_sp03_mt2 - CS 61A Midterm#2 — Your name login...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

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

View Full Document Right Arrow Icon
Background 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 #2 — March 12, 2003 Your name login: cs61a— 06 TA’s name Mfi This exam is worth 40 points, or about 13% of your total course grade. The exam contains 5 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. Question 1 (8 points): What will Scheme print in response to the following expressions? If an expression produces an error message, you may just write “error”; you don’t have to provide the exact text of the message. Also, draw a box and pointer diagram for the value produced by each expression. (cadadr ’((a (b) c) (d (e) f) (g (h) i))) 01> $277! ' i—_i w 2 (cons (append ’ (3) I, 7 (4)) 7 (5));7ET'7f-L1i «:1 ((3 a) S) :11 5 # (cons (append 3 4) 5) WWW (list (cons 3 4) ’(5)) s» 1 r- “7 (2)2171 j /\ . /\ .L ___\ ‘:‘/ ritwfltia r . \ \ , C6. ‘4‘.) (5)) 2:17 ~ 3 /’.- v 'a/ "‘v 3 LL 5 login cs61a— («2' Your name Question 2 (8 points): Write a procedure lastfirst whose argument is a list of sentences (not a deep-list that could contain lists of lists of lists). It should 1:99.131le list of sentences in which every two— word sentence in the argument is included with the two words interchanged, and sentences with other than two words are omitted: : > (lastfirst ’((john lennon) (james paul mccartney) (george harrison) (ringo starr))) ((lennon john) (harrison george) (Starr ringo)) Respect the data abstraction; use the appropriate selectors and constructors for sen- tences and for non-sentence lists. (a) Write a recursive version of lastfirst. Write only one procedure, without helper functions, and without using higher-order procedures. / . \"l‘v’T‘Z‘fp'; (1,; ”L.1/‘ ' 13+) Uri. (Wily-:2. U’W 'Z*""l}l (K 0N3, iLWfill ‘1 ‘3“ "ll } N, f 0 7+ .LL‘Y-*¥’w',g\ " 4’ ,. . fr 1 r' . '~\;’ 3 1‘ ‘ " " * 1—} “on. K... a. x af- fl(+—5{> // 4 r, N \ - I \ .~ , . . to: mm)»; {946, ’ ’ ’ .‘I\, (b) Write a version of lastfirst that uses higher-order procedures and does not use recursion. I r . , (Wis. '34: . V1,” _, S, 'l:’ H?" .\+{: nlyyifwl/wfl Mp; “Yf “‘{Q’ ‘Jxl‘f '\ ‘ l WW / _ ~. r . L . '7\. L277: pr; {"4 NF“ (flag/W / 9/, ‘ (ff (y, (lac/f ire/WW \ A.‘.*J_ '5 .)\'x (1(‘(c‘vrr’2’y‘ .l) r‘,\\ ll"\\\ })> ' ’1) Question 3 (8 points): We are going to use the Tree abstract data type to represent the subdivisions of a paper. My paper 1 Problem Description New Computers Cost — However, when a heading has only one subheading (for example, “Proposed Solution”, - has only “New computers” as a subheading),“ it doesn’t actually make sense to subdi- vide the topic. You will write divisions—reduced, which takes in a Tree (using the datum/ children abstract data type discussed in lecture) in which the/(1W. It outputs a similar tree except that each node that has only a single child is replaced by a node with the datum of the parent, followed by a hyphen, followed by the datum of the child. The node’s children are the child’s children, with their divisions reduced. My paper Write divisions-reduced. "m“ l j 7‘} 1.? ail/”w (l, 1"" “ r m w ,1 . ‘* (((?n¢ _‘ 'a’f'pj*L / \‘/!_‘= "l "f‘ffl’QV/V ‘ I , / _,.\- : “121/ i” (f f i f 1/ fl ‘ I l} " vfrd?&i" / x~ (£2 ‘ i ' / / fl waive DJ ’5.“th (Mitch/2m ( rwr : P, L .. ,(y ' , {a . Pr! , . :7“, {hi/j Vl "L'err-Qifi‘kl [49 Q \ ”I 'I-AK \J— 1V7" («6 [\iiIl-x- 4”,, f-J’HVQUH/‘Dr /n‘/ M x. A , ”Xi \ t 37424:, _. r " ’ Jinan-THU .‘I. —~ . \l J I f jll/JI/l /l ,J; (m m/ee‘) v \3 U , t ., ,4 t u / “ , ‘l _ l, , .1, ,,’Kftg//" UPI”? V‘ c_ f M ’/ )))) Your name {>35on l/N ‘ login cs61a— CL Question 4 (8 points): ‘ Write a predicate procedure deep-car? that takes a symbol and a deep-list (possibly including sublists to any depth) as its arguments.‘ It should return true if and only if the symbol is the car of the list or of some list that’s an element, or an element of an element, etc. i l > (deep-car? ’a"(aflb c)) #t l l > (deep-car? ’a ’(b a c)) l #f v > (deep-car? a ’((x y) (z (a b) c) d))§ #t ' ’(((a)))) $1) > (deep—car? #1: Fill in the blanks below to complete the definition. (define (deep-car? symbol lst) pahf (if (W 151:) (or (eq? symbol __J"y7 XEA'l ) (helper symbol /S+‘ )) $747 )) (define (helper symbol lsts) A (cond ((null? lsts) _:______) OOY ((deepwfigzfi2 symbol (car lsts)) flit ) \ I Ml} "41"‘r (else {\YIQ"\\E:P‘-r fir "(1" (1" - J \ i . Jr) ))) #—CT Question 5 (7 points): In the early 1960s, the IBM 1620 computer did arithmetic one digit at a time, just the way you learned in elementary school, looking up the digit-by—digit sums or products in a table. We’re going to simulate that. You are given a table in which the rows and columns are digits (0 to 9) and what’s stored in each table entry is a two-element list representing the two-digit sum. For example: jay/Him!!!» r C. . (put 2 3 ’(o 5)) (put 8 9 ’(1 7)) ”lay—ism A05 We want a procedure add that takes two nonnegative integers, represented as lists of digits, and adds them, returning another list of digits: $2M fie > (add 1<3 7) ’<4 5)) <8 2) because 37 + 45 = 82. Note: Don’t worry about extra leading zeros in the result. For example, this is okay: > (add ’(3 7) ’(4 5)) (o o 8 2) l‘ _i/ Fill in the blanks below: (define (add n1 n2) (cond ((empty? n1) n2) ((empty? n2) n1) ...
View Full Document

{[ snackBarMessage ]}