This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: University of Waterloo
Midterm Examination #2
CS 135 Term: Winter Year: 2012 Identifying information will be printed here. Date: February 27, 2012
Time: 7:00 pm
Duration: 1 10 minutes
Sections: 001—002
Instructors: Brad Lushman Student Signature: UW Student ID Number: Y Number of Exam Pages  ages
(including this cover sheet) \ C
Exam Type I Closed Book
/
Additional Material Allowed ) NO ADDITIONAL MATERIALS ALLOWED This document is for the exclusive use of x8wen. Marking Scheme (for graders’ use only): Question Max Score Marker 1 10 2 8 3 7 4 6 5 10 __ 6 10 __ \ x g 7 27 ( )
MaxicmtﬁmxTotégéore: 78 Tot‘fll Sgor; CS 135 Midterm Examination #2 This document is for the exclusive use of x8wen. ) Winter 2012 Page 2 of 14 Instructions: 0 All code is to use the Beginning Student with List Abbreviations language. 0 Individual questions will indicate which design recipe components must be included for full marks. Unless
otherwise speciﬁed, you can assume the following: — Helper functions must also include a purpose (in addition to any other design recipe components
mentioned in the question). — Examples and tests must use checkexpect, and unless otherwise allowed, must be different from any
examples supplied in the question. 0 Your functions do not have to check for invalid inputs unless we ask you to do so. 0 Functions you write may use:
— Any function you have written in another part of the same question. — Any function we asked you to write for a previous part of the same question (even if you didn’t do
that part).
— Any other builtin function or special form discussed in lecture, unless speciﬁcally noted in the question. _
— Any builtin mathematical function. \ 0 CS 135 Midterm Examination #2 Winter 2012 Page 3 of 14
This document is for the exclusive use of x8wen. l. (10 points) Consider the list lsr deﬁned by the following expression:
(deﬁne lsr ’((((a) b) C) d)) (a) Write a Scheme deﬁnition of the same list using the function list but not was or quoted lists.
(deﬁne [st (list (list (list (list ’a) ’b) ’c) ’d)) (b) Write a Scheme deﬁnition of the same list using the function cons but not list or quoted lists. (deﬁne [st (cons (cons (cons (cons ’a empty) (cons ’b empty»
(cons ’0 empty» \ \
(ax (cons ’ d empty») (c) Draw a boxandpointer diagram of the same lis . \
a \X
_\ ‘ <1 ) / (d) Give the value of (res: (resr lsr)). empty
(e) Write a Scheme expression using only the functionsﬁrsr and rest and the identiﬁer lsr which
evaluates to ’b. (ﬁrst (rest (ﬁrst (ﬁrst lst)))) CS 135 Midterm Examination #2 Winter 2012 Page 4 of 14
This document is for the exclusive use of x8wen. 2. (8 points) Assume that the following deﬁnitions have already been completely processed in Beginning
Student with List Abbreviations: (deﬁne a 2)
(deﬁne b (* a 4))
(deﬁne (f n)
(cond [(zero? n) empty]
[else (cons (makepom a (+ a b)) (1“(00’0'1 n)))])) (deﬁne (g I)
(cond [(empty? I) 0]
[else (it (rest l))])) (deﬁne (it I)
(cond [(empty? 3) 1] [else (3 (F85! il))])) 
For each of the following Scheme expressions, provide the follow g$nesn o The expression that would result after exactly one s bstit 'on ep, using the substitution rules as
deﬁned in lectures; and 2 o The ﬁnal value that would result. If the evwo would esult in an error, describe the error. (a) 0—3  \
C x __
(cond . '
[(zero? —2_) thpﬂd [else (consihwke—yosn —2 (+ —2 b)) (f (add! —2)))]) ) (list (makepom —2 6) (make—pom —1 7))
(h) (f (— b 0)) (f(— 8a)) Program runs out of memory. (6) (3T1 2 3 4)) CS 135 Midterm Examination #2 Winter 2012 Page 5 of 14
This document is for the exclusive use of x8wen. (cond
[(empty? ’(1 2 3 4)) 0]
[else (h (rest ’(1 2 3 4)))])) 0
(d) (g (f 0)) (g (cond
[(zero? 0) empty]
[else (cons (make—pom 0 (+ 0 19)) (f (addl 0)))]))
0 \ ' \
3. (7 points) Write a function removekey which consumes an ass/63mm ist alst and a key k, and produces the
s association list that is identical to alsr, except that the kej(k a y a sociated value have been removed. i
The key k may or may not actually be in the associa ‘on 115 a! I, ' ut if it does occur, you may assume that it occurs only once. Use this assumption to avo' Lll'll'lt. cessary ecursive calls.
For example, (removekey “((1 "81") (2 "b") (3 "0" pr duces the association list ’((1 "21") (3 "0"».
Include a contract and purpose. ;; remove—key: AL Nu ;; Removes the entry. \Xi‘th th given key from the given AL, if it is there.
(deﬁne (remove—key alﬂt kﬁ/
(cond
[(empty? alsr) empty]
[(: (ﬁrst (ﬁrst alsr» k) (rest 05150]
[else (cons (ﬁrst alsr) (remove—key (rest alst) k))])) CS 135 Midterm Examination #2 Winter 2012 Page 6 of 14
This document is for the exclusive use of x8wen. 4. (6 points) Recall the insertion sort routine that was presented in class: (deﬁne (sort alon)
(cond [(empty? alon) empty]
[else (insert (ﬁrst alon) (sort (rest alon)))])) (deﬁne (insert n alon)
(cond
[(empty? alon) (list n)]
[(< n (ﬁrst al0n)) (eons n al0n)]
[else (cons (ﬁrst alon) (insert it (rest alon)))])) On Assignment 5, you wrote a program to automatically generate an index for a book, by recording, for
each word in the book, the pages on which it occurs. Recall that an index is essentially an association list
in which the key is a String, and the value is a list of Nat: ;; An Index = (listof (list String (listof Nat))) '
(deﬁne sampleindex (list (list "scheme" (list '1 4 9)) \ '
(list “racket” (list 2 7 9 10)))) will write a Scheme function that consumes an Index and rod es an equivalent Index, which is sorted
alphabetically by word. In the space below, ada he i sertion sort routine given above so that it consumes
an Index, and sorts the index alphabetically by wax: Y may use the builtin functions string< ? and On Assignment 5, you were not required to sort the 'nde)(al jitically by word. So on this question you string> ? as part of your solution. Incl' e a co 3 t b th for the function sort, and its helper insert. C ;; sort: Index —> Index (deﬁne (sort alst) \
/ (cond
[(empty? alsthmpty]
[else (insert (ﬁrst alst) (sort (rest alst)))])) ;; insert: (list String (listof Nat)) Index —> Index
(deﬁne (insert p alst)
(cond
[(empty? alst) (list p)]
[(string< ? (ﬁrst p) (ﬁrst (ﬁrst dist)» (cons p alst)]
[else (cons (ﬁrst alst) (insert p (rest alst)))])) CS 135 Midterm Examination #2 Winter 2012 Page 7 of 14
This document is for the exclusive use of x8wen. 5. (10 points) Write the Scheme function removeallneg, which consumes a list of lists of numbers alolon ,
and produces the list of lists of numbers resulting from removing from alolon all lists that only contain
negative numbers. (Note: the empty list can be said to contain only negative numbers. It can also be said to contain only positive numbers. This is an example of a vacuous truth.) For example, (removeallneg
“((‘I 2 3) (4 5) (6 7 8))) should produce ’(('1 2 3) (6 7 8)). Include a contract and purpose. ;; remove—all—neg: (listof (listof Num)) —> (listof (listof Num))
;; Removes all lists from alalon that contain only negative numbers
(deﬁne (remove—allneg alalon)
(cond
[(empty? alalon) empty]
[(all—neg? (ﬁrst (110.4011)) (remove—all—neg (rest alalonD]
[else (cons (ﬁrst alalon) (remove—all—neg (rest alalon)))])) ;; all—neg?: (listof Num) —> Boolean \ i \
g tive ;; Answers true if the list contains only ne
(deﬁne (all—neg? alon) ? (cond \ [(empty? alon) true] [else (and (< (ﬁrst (2(035 0 (all— 6g? (rest alon)))])) ) / CS 135 Midterm Examination #2 Winter 2012 Page 8 of 14
This document is for the exclusive use of x8wen. 6. (10 points) In order to get your degree, you need to pass at least some minimum number of courses (typically
40). In addition, you cannot fail more courses than a speciﬁed limit (in the Math Faculty, that limit is 6).
Write the function cangraduare ?, which consumes a list of grades (which will be natural numbers), a
minimum number of passed courses (a natural number), and a failure limit (also a natural number).
cangraduare? will produce true if the list contains at least the minumum passed courses, and no more than
the given limit of failed courses. A course with a grade of 50 and above is considered passed. A course
with a grade of 49 and below is considered failed. You do not need to deﬁne constants for this question. For example, (cangraduate? “(5O 60 20) 2 1) should produce rrue. For this question, you may not use nontrivial list functions like reverse, append, merrtberﬂ ierzgﬂz, etc. In
addition, you may not use a helper function. Contract, purpose, examples, and tests are not required. CS 135 Midterm Examination #2 Winter 2012 Page 9 of 14
This document is for the exclusive use of x8wen. (deﬁne (can—graduate? grades passes fails)
(cond
[(empty? grades) (zero? passes)]
[(and (zero? passes) (zero ? fails»
(cond
[(>: (ﬁrst grades) 50) (can—graduate? (rest grades) 0 fails)]
[else false])]
[(and (zero? passes) (> fails 0))
(cond
[(>: (ﬁrst grades) 50) (can—graduate? (rest grades) 0 fails)]
[else (can—graduate? (rest grades) 0 (SM fails))])] [(3nd (> passes 0) (zero ?fails)) \ x (cond _
[(>: (ﬁrst grades) 50) (can rddgzﬁ? (rest grades) (sabI passes) 0)] [else false])] \
SQX
5 [(and (> passes 0) (> ail
(cond (% _
[(>: (ﬁrst grates) éacan—gradaate? (rest grades) (sabI passes) fails
[else (can—grad ate ?'_ est grades) passes (sabl fails))])])) /
0r ) (deﬁne (can—graduate? grades passes fails)
(cond
[(empty? grades) (and (<: passes 0) (>:fails 0))]
[(< (ﬁrst grades) 50) (can—graduate? (rest grades) passes (sabI fails))]
[else (can—graduate? (rest grades) (sabI passes) fails)])) CS 135 Midterm Examination #2 Winter 2012 Page 10 of 14
This document is for the exclusive use of x8wen. 7. (27 points) Consider the following Scheme deﬁnitions: (deﬁnestruct 3908 (fr: argl argZD ;; A algebraic expression (AlgExp) is one of ;; * a Num ;; * a Symbol (not one of ’+, ’, ’*, ’z’) ;; * a (makebae Symbol AlgExp AlgExp) where Symbol is one of ’+, ’, ’*, “I (deﬁne myexp (makebag 5* (makebag ’+ ’x 1) (makebag ’— ’X (makebag “f ‘I ’y)))) (a) (4 points) Draw the tree structure encoded by the constant myexp, declared above. (b) (3 points) Using ordinary mathematical notatio givehgejligebraic expression that the constant
myexp, declared above, represents. \ (Hue—My) C C ' (c) (3 points) Give an expressidp involvi g myexp that produces the ’y from myexp. /
(bateargZ (bae—ar (bate—argZ my—exp)» (d) (4 points) Give an encoding of the algebraic expression .132 + 2.1? + 3 as an AlgExp, using the
declarations given above. CS 135 Midterm Examination #2 Winter 2012 Page 11 of 14
This document is for the exclusive use of x8wen. (make—bag 3+ (make—baa ’+ (make—bag 3* ’X ’X) (makebag ’>I< 2 ’X)) 3)
0r (make—bag ’+ (make—baa ’>I< ’X ’X) (make—baa ’+ (make—bag ’>I< 2 ’X) 3))
( x
\
C x __
\ ‘ <
) /
CS 135 Midterm Examination #2 Winter 2012 Page 12 of 14 This document is for the exclusive use of x8wen. (e) (5 points) Write a template for a function that consumes an AlgExp. Do not include a contract. (deﬁne (my—AlgExp—fn aexp)
(cond [(number? aexp) [(Symbol? aexp) . . .] [else
. . . (baefn aexp) . . .
. . . (myAlgExp—ﬁz (buearg] aexp)) . . .
. . . (myAlgExp—ﬁz (baeargZ aexp)) . . . D) (f) (8 points) Write the function containsvar? that consumes an AlgExp ate and a Symbol var (not one
of ’+, ’—, ’*, ’1’), and produces true if the expression ae contains at least one occurrence of the
variable var, andfalse if it doesn’t. Include a contract and enough tests to fully exercise your c6?
e ;; contains—var?: AlgEXp Symbol 1%
(deﬁne (contains—var? we v)
(cond \
[(number? ae)false}) \
[(Symbol? ae) (sygnbof ? a 12)]
[else (or (containstgr? bue—argl we) v) (contains—var? (baeargZ ae) v (check—expect (com? thevar? 2 ’X) false)
(check—expect (contxinS—var? ’X ’X) true)
(check—expect (contains—var? ’y ’X) false) (check—expect (contains—var? (makebate ’—— ’X ’y) ’X) tme) (check—expect (contains—var? (makebate ’—— ’y ’2) ’X)false) CS 135 Midterm Examination #2 Winter 2012 Page 13 of 14
This document is for the exclusive use of x8wen. (Extra space for any question, if needed) CS 135 Midterm Examination #2 Winter 2012 Page 14 of 14
This document is for the exclusive use of x8wen. ...
View
Full Document
 Winter '07
 VASIGA
 Expression, Midterm Examination

Click to edit the document details