mid2sol - University of Waterloo Midterm Examination #2 CS...

Info iconThis preview shows pages 1–14. 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 DocumentRight 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 DocumentRight 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 DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

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

View Full DocumentRight Arrow Icon
Background image of page 14
This is the end of the preview. Sign up to access the rest of the 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 ( ) MaxicmtfimxToté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 specified, 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 check-expect, 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 built-in function or special form discussed in lecture, unless specifically noted in the question. _ — Any built-in 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 defined by the following expression: (define lsr ’((((a) b) C) d)) (a) Write a Scheme definition of the same list using the function list but not was or quoted lists. (define [st (list (list (list (list ’a) ’b) ’c) ’d)) (b) Write a Scheme definition of the same list using the function cons but not list or quoted lists. (define [st (cons (cons (cons (cons ’a empty) (cons ’b empty» (cons ’0 empty» \ \ (ax (cons ’ d empty») (c) Draw a box-and-pointer 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 functionsfirsr and rest and the identifier lsr which evaluates to ’b. (first (rest (first (first 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 definitions have already been completely processed in Beginning Student with List Abbreviations: (define a 2) (define b (* a 4)) (define (f n) (cond [(zero? n) empty] [else (cons (make-pom a (+ a b)) (1“(00’0'1 n)))])) (define (g I) (cond [(empty? I) 0] [else (it (rest l))])) (define (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 defined in lectures; and 2 o The final value that would result. If the evwo would esult in an error, describe the error. (a) 0—3 -- \ C x __ (cond . ' [(zero? —2_) thpfld [else (consihwke—yosn —2 (+ —2 b)) (f (add! —2)))]) ) (list (make-pom —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 remove-key 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, (remove-key “((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. (define (remove—key alflt kfi/ (cond [(empty? alsr) empty] [(: (first (first alsr» k) (rest 05150] [else (cons (first 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: (define (sort alon) (cond [(empty? alon) empty] [else (insert (first alon) (sort (rest alon)))])) (define (insert n alon) (cond [(empty? alon) (list n)] [(< n (first al0n)) (eons n al0n)] [else (cons (first 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))) ' (define sample-index (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 built-in 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 (define (sort alst) \ / (cond [(empty? alsthmpty] [else (insert (first alst) (sort (rest alst)))])) ;; insert: (list String (listof Nat)) Index —> Index (define (insert p alst) (cond [(empty? alst) (list p)] [(string< ? (first p) (first (first dist)» (cons p alst)] [else (cons (first 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 remove-all-neg, 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, (remove-all-neg “((‘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 (define (remove—all-neg alalon) (cond [(empty? alalon) empty] [(all—neg? (first (110.4011)) (remove—all—neg (rest alalonD] [else (cons (first alalon) (remove—all—neg (rest alalon)))])) ;; all—neg?: (listof Num) —> Boolean \ i \ g tive ;; Answers true if the list contains only ne (define (all—neg? alon) ? (cond \ [(empty? alon) true] [else (and (< (first (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 specified limit (in the Math Faculty, that limit is 6). Write the function can-graduare ?, 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). can-graduare? 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 define constants for this question. For example, (can-graduate? “(5O 60 20) 2 1) should produce rrue. For this question, you may not use non-trivial list functions like reverse, append, merrtberfl ierzgflz, 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. (define (can—graduate? grades passes fails) (cond [(empty? grades) (zero? passes)] [(and (zero? passes) (zero ? fails» (cond [(>: (first grades) 50) (can—graduate? (rest grades) 0 fails)] [else false])] [(and (zero? passes) (> fails 0)) (cond [(>: (first grades) 50) (can—graduate? (rest grades) 0 fails)] [else (can—graduate? (rest grades) 0 (SM fails))])] [(3nd (> passes 0) (zero ?fails)) \ x (cond _ [(>: (first grades) 50) (can rddgzfi? (rest grades) (sabI passes) 0)] [else false])] \ SQX 5 [(and (> passes 0) (> ail (cond (% _ [(>: (first grates) éacan—gradaate? (rest grades) (sabI passes) fails [else (can—grad ate ?'_ est grades) passes (sabl fails))])])) / 0r ) (define (can—graduate? grades passes fails) (cond [(empty? grades) (and (<: passes 0) (>:fails 0))] [(< (first 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 definitions: (define-struct 3908 (fr: argl argZD ;; A algebraic expression (AlgExp) is one of ;; * a Num ;; * a Symbol (not one of ’+, ’-, ’*, ’z’) ;; * a (make-bae Symbol AlgExp AlgExp) where Symbol is one of ’+, ’-, ’*, “I (define my-exp (make-bag 5* (make-bag ’+ ’x 1) (make-bag ’— ’X (make-bag “f ‘I ’y)))) (a) (4 points) Draw the tree structure encoded by the constant my-exp, declared above. (b) (3 points) Using ordinary mathematical notatio givehgejligebraic expression that the constant my-exp, declared above, represents. \ (Hue—My) C C '- (c) (3 points) Give an expressidp involvi g my-exp that produces the ’y from my-exp. / (bate-argZ (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) (make-bag ’>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. (define (my—AlgExp—fn aexp) (cond [(number? aexp) [(Symbol? aexp) . . .] [else . . . (bae-fn aexp) . . . . . . (my-AlgExp—fiz (bue-arg] aexp)) . . . . . . (my-AlgExp—fiz (bae-argZ aexp)) . . . D) (f) (8 points) Write the function contains-var? 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% (define (contains—var? we v) (cond \ [(number? ae)false}) \ [(Symbol? ae) (sygnbof ? a 12)] [else (or (contains-tgr? bue—argl we) v) (contains—var? (bae-argZ ae) v (check—expect (com? the-var? 2 ’X) false) (check—expect (contxinS—var? ’X ’X) true) (check—expect (contains—var? ’y ’X) false) (check—expect (contains—var? (make-bate ’—— ’X ’y) ’X) tme) (check—expect (contains—var? (make-bate ’—— ’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

This note was uploaded on 04/02/2012 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.

Page1 / 14

mid2sol - University of Waterloo Midterm Examination #2 CS...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online