midterm2-w10-sol

midterm2-w10-sol - University of Waterloo Midterm...

Info iconThis preview shows pages 1–15. 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
Background image of page 15
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: 2011 Identifying information will be printed here. Date: February 28, 201 1 Time: 7:00 pm Duration: 120 minutes Sections: 001—002 Instructors: Brad Lushman Student Signature: UW Student ID Number: - i Number of Esam Pages I paves " (including this cover sheet) \ / Esam Type I; losed Book Additional Material Allowed 3 NO ADDITIONAL MATERIALS ALLOWED This document is for the exclusive use of x284”. Marking Scheme (for graders’ use only): _-- 3 11 4 7 - _ 4 Maximum Total Score: 90 Total Seore: Instructions: o All code is to use the Beginning Student with List Ahl‘n'eyiaticms language. In Indiyidual questions will indicate which design recipe components must he 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]. — Esamples and tests must use citet's—eqiect. and unless otherwise allowed. must be different from any esarnples supplied in the question. In Your functions do not haye to check for inyalid inputs unless we ask yet] to do so. In Functions you write may use: — Any function you haye written in another part of the same question. — Any function we asked you to write for a preyious part of the same question teyen if you didn‘t do that part}. — Any other huilt—in function or special form discussed in lecture. unless specifically noted in the question. — Any huilt—in mathematical function. CS [35 Midterm Examination #2 ‘Winter 2011 Page 2 of IS. ThlS document IS for the excluswe use of x284“. l. (10 points) Censider the list ls: defined by the fellewing eapressien: (define Is: "((((a b) C) d) 6)) (3) Write a Seheme definitien ef the same list using the funetien list but net eeas er queted lists. (define [31‘ (list (list (list (list ’a ’b) ’e) ’d) ’e)) (h) Write a Seheme definitien ef the same list using the funetien was but net liar er queted lists. (define [31‘ (cans (com (com (cons "a (com ’b empty» (cores *0 empty» (com ’d empty» > (cons ’ e empty» \ \\\ fl -<‘ J ,E C Ill \/ (e) Draw a bea-and-peinter diagram ef the Zamblist. (d) Give the value ef (rear (rest 330). amp I): (e) Write a Seheme eapressien using enlv the funetiensfirsr and rest and the identifier Est vvhieh evaluates te ’b. CS 135 Midterm Examinatiun #2 Winter 2011 Page 3 uf 15 Thls document IS for the excluswe use of x284“. (fir/'5: (rest (first (fir/'5: (first #0))» CS 135 Midterm Examination #2 Winter 2011 Page 4 (If 15 ThlS document IS for the excluswe use of x284“. 2. (10 points) Assume that the following definitions have already been completely processed in Beginning Student with List Abbreviations: (define a 2) (define b (a: a 4)) (define (f a) (cond [(aero? a) EFHPEV] [else (cons (make-pose a (—l— a 5)) 0“ (add! H)))l)) (define (g I) (cond [(empo? 3) 0] [else (it (first I) (second 3) (rest (rest Z)))])) (define (h a E? c) > 1 (+ (— 3? £1) (a CD) \ Fer each of the following Scheme eapressions, provide o1 {:rin two lines: Ye I The eapression that would result after efctlyjoue swim 'on step, using the substitution rules as defined in lectures; and I The final value that would result. If" evalélticgould result in an error, describe the error. (a) (f —2) g (cond \ / [(aem? —2) amp ] if [else (cons (make—pose —2 (+ —2 33)) (f (crde (list (make-pose —2 6) (make—pose —l 7)) (I?) (f (— E? 3)) (f(— 8a)) Program runs out of memory. CS 135 Midterm Examination #2 Winter 2011 Page 5 of 15 Thls document IS for the excluswe use of x284“. (C) (£11 2 3 4)) (cond [(empry? ’(‘I 2 3 4)) 0] [else (h (first ’(1 2 3 4)) (second '(1 2 3 4)) (my (rest 3*(‘I 2 3 2 (d) (-1- :11 (g (reg: “(1 2 3 4 5 E)))) (+ 2 (g (rest ’(‘I 2 3 4 5 6)))) Ermr: rakes rest of an empty list (3 (cond ( _ / [(gem P’ 0) empty] [else (cons (makegpa7 0 (f (uddl \ / 0 CS 135 Midterm Examinatiun #2 Winter 2011 Page 6 {1f 15 ThlS document IS for the excluswe use of x284“. 3. (11 points) A sequence cf numbers is said tc be increasing if each number in the the sequence is greater than its predecesscr. If a sequence cf numbers is net strictly increasing, we might be satisfied if it is at least “mcstly increasing”. We say that a sequence :11, e2, . . . , [1,, is mestly increasing if a, E s' fcr s' = 1, . . . ,n. Fer example, the list (Ji3r1 4385109812) is mcstly increasing (because 1 E 1, 4 E 2, 3 E 3, 8 E 4, etc.). Alsc, by definiticn, the empty list is ccnsidered mcstly increasing. Write a Scheme functicn malady-increasing? that ccnsumes a list [11, . . . , [1,, cf numbers and prcduces true if and cnly if the sequence is mcstly increasing. Include a cuntract, purpese, and encugh tests tc ccmpletely exercise ycur functicn. If ycu create helper functicns, include a contract and purpose fer each helper. ;; mestly—increasing?: (listef Num) a Beelean ;; Determines whether the given list of numbers is mostly increasing. (define (manly—increasing? dist) > (m-i-help ulst 1)) \ \ (Check-expect (mostly-increasing? emgxty) titre I}? Irate) (check-expect (mestly-incret?ng ((list 0) false) ;; m—i—help: (listef Num, lgu 4 eelean ;; Determines whether,__f r ea , the i—th element of alst is at least (ChECk-EXpECI (mostly-increasifigfili ;; n+i—l. I ' (define (m-i-help ulst n) B (cend [(empty? alsr) true] [(>: (first (£15.!) 11) (m—i—help (rest ulst) (add! [else felseD) CS 135 Midterm Examinatinn #2 Winter 2011 Page 7 cf 15 Thls document Is for the excluswe use of x284“. 4. (7 paints) Write a fancticn FEmfll’E-kt‘f}? which ccnanmea an aaacciaticn liat afar and a key it, and prcdncea the aaacciaticn list that ia identical tc clan except that the key It and its aaacciated yalne have been remcyedi The key It may er may nct actually be in the aaacciaticn list (1331*, but if it dcea cccnr, yen may assume that it cccnra cnly cncei Uae thia aaanmpticn tc aycicl nnneceaaary recursive calla. Fer example, (Tammie-key ’(('l "51”) (2 "b”) (3 “C”)) 2) prcdncea the aaacciaticn list “((1 "21") (3 “c”)). Include a ccntract and purpcaei ;; remove—key: AL Num —> AL ;; Removes the entry with the given key frcm the given AL, if it is there. (define (remove-key ails: k) (cond [(empty? afar) empty] [(: (first (first (£150) k) (rest alga] [else (60113 (fimf afar) (remave—key (re athDD) < V. )l/ a \B 33/ CS 135 Midterm Examinaticn #2 Winter 2011 Page 8 cf 15 Thls document IS for the excluswe use of x284“. 5. (20 points) 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. Realistically, however, some words are so common, and carry such little meaning, that to have them in the index is not particularly helpfuli 1Words of this kind are called stcpwcrds, and include words like the, c, are, if, cr; ear, bar, emf, is, cfl etc. We tend to omit stopwcrds from an index because they carry no information contenta and many of them appear on every page of the book anywayi (a) (12 points) Write a Scheme function remcve-stcpwcrds that consumes a (Eisrcf (tisrcf stricg)) back and a (Eisrcf strmg) arcpwcrds and produces the (Eisrcf (tisrcf stricg)) resulting from removing from back every string that matches a string in arcpwcrds. Include a contract, purpcse, and three examples. If you write helper functions, include a contract and purpnse for each helper You may use the built-in Scheme function member? in your solution. (member? e Est) answers tree it e is an element of tar, and false otherwise. ;; remove—stopwords : (listof (listof string)) (listof string) ;; A; (listof (listof string)) > ;; Removes stopwords (occurring in th Nlist) from the given book (define (remcve—srapwards aback Is) (cond ._ [(empty? aback) empty]( . / [else (carts (remavesyaée (first aback) sws) P (remavé-sta ward (rest aback) sves))])) (cheek—expect (remaleSt crds '0 *(”a” "the")) (cheek—expect (remavm—Isziapwards ’(()) ’(”a” “the”)) ’(())) (check—expect (FEMOVE-SIOPWOFJS ’((”the” “story”)) ’(”a” "the")) ’((”story”)j ;; remove—sw—page : (listof string) (listof string) a (listof string) ;; Removes all stopwords (occuring in the given list) from the given page (define (remcve—sw-page apage awe) (cond [(empry? vpage) empty] [(member? (first apage) sws) (remcve—sw—page (rest apage) sws)] [else (carts (first apage) (remave-sw-page (rest apage) SWSHD CS 135 Midterm Examination #2 Winter 2011 Page 9 of 15 Thls document IS for the excluswe use of x284“. (h) (3 peints) In part (a), yeu vvrete a funeti en te strip stepvverds frem a beek, se that the result eeuld be used te ereate an index witheut stepwerds. Anether appreaeh te the same preblem vveuld be te ereate the index first, and then remeve frem it all stepvverd entries, Write the Seheme funetien renteve-index-srepwerds that eensumes an index (ie, a (Eistef (list string (tistef Net») and a list ef stepvverds, and preduees the index that results frem remeving all stepvverd entries frem the given indea. Observe the fellevving netes: I Yeu may assume that the index and list ef stepvverds are presented in serted, inereasin g vverd erder, I and that ne vverd appears tvviee in either the index er the list ef stepvverds. I Fer full marks, yeu sheuld use beth ef these faets te yeur advantage, se that neither list is traversed mere than enee. I De net use the built-in Scheme funetien memberl Instead, select and use the apprepri ate reeursien pattern fer funetiens that eperate ever tvve lists. I Hevvever, partial credit is available fer a eerreet selutien that uses member? and traverses the lists mere than enee. Inelude a eentraet and purpese. > ;; remeve—index—stepwerds : (liste (li\sst\r’ U (listef Nat))) (listef string) ;; 4,» (listef (list string (listef N -t ;; Removes all of the given st we ds fgpm the given index. (define (remove-index-step@erds ' r: sws) (cend - < [(01‘ (empty? idxgw pry? 51/1/15» idx] [(strirzg<? (firle (firs 1' ) (first SW5» (certs (firSt'tdx) emeve—index—stepwerds (rest idx) swsD] [(strirrg: ? (firstkfirst J'de (first 51423)) (remeve—index—stepwards (rest idx) (rest swsD] [else (remeve—iadex—stepwerds idx (rest sws))])) CS 135 Midterm Examinatien #2 Winter 2011 Page 10 ef 15 Thls document IS for the excluswe use of x284“. 6. (32 pcints) Ccnsider the fellcwing Scheme definitions: (define-struct has (fa erg} arg2)) ;; A algebraic espressicn (AlgEsp) is cne cf ;; ’i‘ a Nurn ;; * a Symbcl ;; e a (rnake-bae Syrnbcl AlgEKP AlsEKP) ;; where Symbcl is cne cf ’+, H “ii... '7 (define nay-exp (make-bee ’4: (make-baa ’— ’X ‘y) (make-bee ’-|— ’X ’30)) (a) (3 pcints) Draw the tree structure enccded by the ccnstant ray-exp, declared abcve. x y x y "3 /% (b) (2 pcints) Using crdinary mathematical cta 'en; ‘ve the algebraic espressicn that the ccnstant nay-exp, declared abcve, represents. " 6 C (a—y)($+y) / f (c) (4 pcints) Give an enccding mike algebraic expressicn a3 + 2c —|— 1 as an AlgEsp, using the declaraticns given abcve. (make-bee 3+ (make-bee ’+ (make-bee 5+: ’X ’X) (marks-bare is: 2 50) 1) 0r (make-bee 3+ (make-bee is ’X ’X) (make-bee ’+ (make-baa 3% 2 ’X) 1)) (d) (5 pcints) Write a. template fer a functicn that ccnsurnes an AlgEsp. Dc nct include a ccntract. CS 135 Midterm Examinaticn #2 Winter 2011 Page 11 cf 15 Thls document IS for the excluswe use of x284“. (define (my—AlgExp—fil aexp) (cend [(number? aexp) [(Symbel? uexp) . . . ] [else . . . (bee-fit uexp) . . . . .. (my-AlgExp-fn (bee-erg] uexpD . .. . .. (my-AlgExp—fi: (bue-argZ uexpD . . . CS 135 Midterm Examinatien #2 Winter 2011 Page 12 {1f 15 ThlS document IS for the excluswe use of x284“. (e) (9 points) Write the function emoe, which consumes two lists leaf} and le12, and produces a list containing those elements that occur in at at least one of let.i and 3332. You may assume that each of £3!!! and let? individually contain no duplicate entries, and you must ensure that the list you produce contains no duplicate entries. You may use the built-in Scheme function member? as part of your solution, (member? e let) produces true if e is an element of let, and firlee otherwise For example, (enter: “(1 2 3) “(2 3 4)) should produce a list that contains the numbers 1, 2, 3, 4 in some order (the exact order does not matter), without duplication, Provide a contract, purpose, and enough tests to fully exercise your code. ;; union: (listof Any) (listof Any) —r- (listof Any) ;; Produces the set—theoretic union of the two given lists. (define (timer; let] let2) (cend [(empry? lefl) leIZ] [(member? (fire: let!) ler2) (unto (Pe I led) letZ)] [else (eerie (fire: let!) (enlen (r et le ) . (check—expect (enter: ’0 ’(1 23)) 'lfl (check—expect (enter: ’(4) ’ 33)) 74 2 3)) i E3» 3(1 2 3)) (f) (9 points) Write the function veg-mi ti at cons es an AlgEsp ee and produces a duplicate-free list of all of the variable names tag re m n/tioeryd in ee. Remember that you are allowed to use functions (check—expect (enter; you were asked to write else ere on thi earn as helpers (even if you did not complete those functions). Include a contract, purpose, 33d enough tests to fully esercise your code. CS 135 Midterm Examination #2 Winter 2011 Page 13 of 15 Thls document Is for the excluswe use of x284“. ;; vars—of: AlgEXp we (listof Symbol) ;; Produces a duplicate—free list of all of the variable names mentioned in ;; the given AlgExp. (define (vars-0f we) (eond [(number? (we) [(Symbol ? ate) (list (16)] [else (union (vars—0f (bee—erg] we» (vars—0f (bue—argZ ae)))])) (Check—expect (vars—0f 2) (Check—expect (vars-0f ’X) ’00) (Check—expect (vars—0f (make—bee 3+ ’2’ '34:;0: y)) (Check—expect (vars-0f (make-bee Ur ’ ’X ’00) \ O \/ a O c " \3/ CS 135 Midterm Examination #2 Winter 2011 Page 14 of 15 Thls document IS for the excluswe use of 3:284“. [Extra epnee fer an}! queetien, if needed] (33 135 h-“Iidterm limminutitm #2 Vb‘rintflr 2011 Page 15 ef 15 ThlS document Is for the excluswe use of x284“. ...
View Full Document

Page1 / 15

midterm2-w10-sol - University of Waterloo Midterm...

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

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