Unformatted text preview: For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail UWaterloo CS115 Final Exam Question s
last update Apr 19 th , 2009
copy right 2009 O da_Ea gle P roduction, all right reserved
CS115/116/125/134/135/136/241/240/341/343 Tutor
odaea [email protected]
Part 1. Arithmetic Expressionw2009
Part 2. Arithmetic SExpressionw2009
Part 3. Association Listsw2009
Part 4. Binary Search Treew2009
Part 5. Productsw2009
Part 6. SExpressionsw2009
Part 7. Taxonsw2009
Part 8. TopThreesw2009
Part 9. Buildin Functionsw2009
Part 10. Selected Functions and Common Mistakew2009
Part 11. Suggested Lab Exercisesw2009 For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part I. Arithmetic Expression
(definestruct ae ( fn args ) )
;; An aexp is a number or (makeae op exlist), where op is a symbol and exlist is an
;; aexplist.
;; An aexplist is empty or (cons ex exlist), where ex is an aexp and exlist is an aexplist.
1. translate the following Arithmetic expression to regular form ( e.g. 3 * ( 5 + 4 ) )
(a) ( define as ( makeae 'add ( cons ( makeae 'sub ( cons 5 ( cons 4 empty ) ) )
( cons ( makeae 'mul ( cons 3 ( cons 2 empty ) ) ) empty ) ) ) )
(b) ( define as2 ( makeae 'sub ( cons ( makeae 'add ( cons 5 ( cons 4 ( cons 2 empty ) ) ) )
( cons ( makeae 'div ( cons 9 ( cons 3 empty ) ) ) empty ) ) ) ) 2. convert the following equation to Arithmetic Expression
(a) ( 1 + 2 + 3 + 4 + 5 )
(b) ( 4 + 2 ) / ( 5 – 3 )
3. write a Scheme function that takes a Arithmetic Expression and output this expression in
regular form just like question1 ( assume each list contains only two elements ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 2. Arithmetic SExpression
;; An asexp is a number or (cons op slist), where op is a symbol and slist is an
;; asexplist.
;; An asexplist is empty or (cons s slist), where s is an asexp and slist is an asexplist. For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 3. Association Lists
;;An association list al is empty or (cons (list k v) alst) where k is an integer key, v is a
;;string value, and alst is an al.
1. implement a function that given a Association List, return a list representing
all the keys in this list.
;; assume ( define aal ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( getkey aal ) → ( list 1 2 3 ) 2. implement a function that given a Association List, return a list representing
all the values in this list.
;; assume ( define aal ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( getval aal ) → ( list “a” “b” “c” ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail
3. implements a function that takes an Association List and an Integer representing a key,
return the value corresponding to the key, false otherwise.
;; assume ( define aal ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( retrieve aal 1 ) → “a”
;; ( retrieve aal 5 ) → false 4. Implements a function that take two Association List, return their intersection if any
feel free to use any functions you have already implemented as a helper function
;; assume ( define al1 ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; assume ( define al2 ( cons ( list 4 “d” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( getintersection al1 al2 ) → ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail
5. Implements a function that take two Association List, return their Union if any
feel free to use any functions you have already implemented as a helper function
;; assume ( define al1 ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; assume ( define al2 ( cons ( list 4 “d” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( getunion al1 al2 ) → ( list ( list 1 “a” ) ( list 2 “b” ) ( list 3 “c” ) ( list 4 “d” ) ) 6. Implements a function that take an Association List and an Integer representing a key
remove the key and the value represented by this key if there exist such key in the list
;; assume ( define aal ( cons ( list 1 “a” ) ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) ) )
;; ( delete aal 1 ) → ( cons ( list 2 “b” ) ( cons ( list 3 “c” empty ) ) ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 4. Binary Search Tree
(definestruct node ( key val left right ) )
;; A binary search tree bst is empty or (makenode k v lft rgt) where k is an integer key,
;; v is a string value, and lft and rgt are of type bst, and all keys in lft are less than k
;; and all keys in rgt are greater than k.
;; assume a tree
( define bst ( maketree 50 “” ( maketree 25 “” ( maketree 12 “” empty empty )
( maketree 37 “” empty empty ) )
( maketree 75 “” ( maketree 57 “” empty empty )
( maketree 85 “” empty empty ) ) ) )
1. implements a function that given a BST and a key value, return this tree node if this key
value exists, return false otherwise
;; ( find bst 75 ) → ( maketree 75 “” ( maketree 57 “” empty empty )
;;
( maketree 85 “” empty empty ) )
;; ( find bst 1 ) → false 2. implements a function that given a BST and return number of nodes in this tree
;; example: ( numofnode bst ) → 7 For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail
3. implements a function that given a BST and return a list representing the preorder iteration
( a preorder iteration is visit the root first, then left subtree, and right subtree ) 4. implements the function defined in question 3 again, but this time, return a list representing
inorder iteration and postorder iteration
( inorder iteration: visit left subtree first, then root, then right subtree )
( postorder iteration: visit left subtree first, then right subtree, then the root )
( you can just explain what part of code need to be changed in question 3 ) 4. implements a function that take a BST and a integer indicating the key and a string
indicating a value, insert this key and its corresponding value to the BST For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 5. Products
;; A productlist is either empty or it is ( cons sp pl ), where sp is a salesproduct and pl
;; is a productlist (definestruct singleproduct ( name origin ) )
;; A singleproduct is a structure (makesingleproduct n o), where n is a string, and o is
;; a string denoting the country of origin. (definestruct salesproduct ( ID prod ) )
;; A salesproduct is a structure (makesalesproduct i p), where i is an integer and p is
;; either a singleproduct or a productlist.
(define plist (list (makesalesproduct 102 (makesingleproduct "p1" "Can"))
(makesalesproduct 103 (makesingleproduct "p2" "US"))))
(define sp1 (makesalesproduct 101 plist))
1. given a salesproduct and a single product, return true if this single product exists in salesproduct, false otherwise.
;; example: ( iscontainproduct sp1 ( makesingleproduct “p1” “Can” ) ) → true
;; example: ( iscontainproduct sp1 ( makesingleproduct “p2” “Chn” ) ) → false; For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 6. Sexpressions
;; An sexp is either empty, or it is (cons s1 s2), where s1 and s2 are both sexps, or it is
;; (cons v s), where v is not a cons and s is an sexp. For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 7. Taxons
(definestruct tmodern (name pop))
;; A tmodern is a (maketmodern n p), where n is a string and p is a number. (definestruct tancient (name age left right))
;; A tancient is a (maketancient n a l r), where n is a string, a is a number, and l and r
;; are taxons.
;; A taxon is a tmodern or a tancient.
;; ( define t ( maketancient “animal” 500 ( maketmodern “human” 60 )
( maketmodern “cat” 5 ) ) )
1. write a function that takes a taxon and calculate the total population
;; example: ( totalpopulation t ) → 65 2. write a function that takes a taxon and return a list representing all tmodern
;; example: ( listofmodern t ) → ( list ( maketmodern “human” 60 )
;;
( maketmodern “cat” 5 ) ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 8. TopThrees
(definestruct topthree (name fav1 fav2 fav3))
;; A topthree is a structure (maketopthree n f1 f2 f3), where n (for the person’s name),
and f1, f2, f3 (for the person’s favorite three movies) are all strings.
1. write a function that takes a list of topthree and a string indicating a name for a movie, and
return number of people in favor of this movie. 2. write a function that takes two topthree structures and return number of favorite movie in
common.
;; ( define p1 ( maketopthree “A” “die hard” “gump” “the core” ) )
;; ( define p2 ( maketopthree “B” “gump” “simpsons” “the core” ) )
;; example: ( numincommon p1 p2 ) → 2 For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 9. Buildin Functions
map:
filter:
foldr:
reverse:
buildlist:
1. for each row in the table below, determine what would happen if you opened DrScheme
and tried to evaluate the given code. If there is an error, briefly explain how Scheme's Syntax
rules are violated or describe what goes wrong when the code is run. If there is no error, give
the value produced by DrScheme. The first two rows have been completed for you.
Assume you already have: ( define alon ( list 1 2 3 4 5 ) )
( map add1 alon )
( map even? alon )
( filter add1 alon )
( filter even? alon )
( foldr + 0 alon )
( foldr + 5 alon )
( reverse alon )
( buildlist 5 even? )
2. choose which buildin functions to use for the following problem, do not write the function
body, some problem may need two or more of given buildin functions.
(a) given a list of numbers, output all prime numbers only in this list.
;; example: ( question_a ( list 1 2 3 4 5 ) ) → ( list 2 3 5 )
(b) given a list of numbers, output all numbers mod 5
;; example: ( question_b ( list 3 7 12 6 1 ) → ( list 3 2 2 1 1 )
(c) given a list of cards( makecard value suit ), output a list of boolean
expressions that indicate whether each card is ace
;; example: ( question_c ( list ( makecard 1 'spades ) ( makecard 2 'hearts ) )
;;
→ ( list true false )
(d) given a natural number and calculate factorial for this number
;; example: ( question_d 4 ) → 24
;; example: ( question_d 1 ) → 1
(e) given a list of numbers, output true if all number is even false otherwise
;; example: ( question_e ( list 2 4 6 ) ) → true For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail
;; ( question_e ( list 1 2 3 ) ) → false (f) given a natural number and output list of even numbers smaller or equal
to this number
;; example: ( question_f 5 ) → ( list 4 2 )
;; example: ( question_f 10 ) → ( list 10 8 6 4 2 )
(g) given a list of strings, output a string that contain all strings in list
;; example: ( question_e ( list “a” “b” “c” ) ) → “abc”
3. implements functions using buildin functions
(a) implements 2(d) 2(e) 2(f) and 2(g), no recursion allowed, only buildin
functions can be used. (b) given a list of cards, and sort them by both suit and value
spades > hearts > clubs > diamonds
you can assume function ( card>? card1 card2 ) exists which take two cards, return
true if card1 > card2, false otherwise. For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 10. Selected Functions and Common Mistake
Stringrelated Functions
1. (string=? s1 s2) produces true if s1 and s2 are characterbycharacter the same
2. (stringlength s) produces the number of characters in s
3. (substring s p1 p2) produces the string containing all the characters from position
p1 to p21
Other Selected Functions
1. (add1 x) produces x+1
2. (checkexpect act exp) test passes if act and exp are equal, fails otherwise
3. (equal? e1 e2) produces true if e1 and e2 are equal
4. (max x y) produces x if x > y, and y otherwise
5. (sub1 x) produces x1
6. (append l1 l2 … ln) produces a list containing all the values in l1, l2, …, ln, in that
order. Accepts 2 or more lists.
1. fill in the blank with what happened if each line is evaluated.
( string=? 'abc 'abc )
( stringlength “abc” )
( substring “abcde” 0 2 )
( substring “abcde” 0 5 )
( equal? 'abc 'abc )
( equal? “abc” “abc” )
( cons ( list 9 ) ( list 9 ) )
( cons ( list 9 ) ( cons ( list 9 ) empty )
empty ) )
( list ( list 9 ) ( list 9 ) )
( list ( cons 9 empty ) ( cons 9 empty ) )
( append ( list 9 ) ( list 9 ) )
( append ( list 9 ) 9 ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 5197291582 for detail Part 11. Suggested Lab Exercises
Winter 2009
complete the following lab exercises using local and builtin abstract
functions, as seems appropriate
1. The airport parking lot has rates by the week and by the day, where you pay by the weekly rate of
$74.95 for each complete week (any consecutive seven days) and by the daily rate of $14.95 for any
remaining days. Create a function airportparking that consumes an integer number of days and
produces the bill. Be sure to use constants where appropriate. Note: don’t worry if you produce a
number that looks like 5 instead of 5.00 or a number that looks like 5.1 instead of 5.10.
2. In this question you should be making use of several helper functions. Create a function filmchoice
that produces a string indicating the costs of creating a film based on two pricing options. The function
filmchoice consumes the number of clips, the length of the film, and information for two options. Each
option is given as a breakpoint (the number of clips charged at the full cost of $100 each) and a
discount
3. A clock is a structure (makeclock h m) where h is an integer in the range 0 to 23 and m is an integer
in the range 0 to 59; the structure definition is (definestruct clock (hours mins)). Create a function dur
that consumes two clock structures and produces an integer indicating the number of minutes elapsed
between two times. If the second time is later than the first, you can assume that both times are on the
same day. If the second time is earlier than the first, you can assume that the first time is on one day
and the second time is on the next day. One way to approach this problem is to write a helper function
that determines if two times are on the same day.
4. Create a function evenlengthnumber that consumes a list of strings and produces the number of
strings in the list that have even length.
5. Create a function switchcase that consumes a string and produces a string in which each lowercase
letter is converted to an uppercase letter, each uppercase letter is converted to a lowercase letter, and
all other characters are preserved. You should make use of a helper function that processes a list of
characters.
6. An event is a structure (makeevent t d) where t is a symbol (the type of event) and d is a duration in
minutes; the structure definition is (definestruct event (type dur)). Create a function totaldur that
consumes a list of events and produces the total duration in minutes.
7. Recall the structure card, a structure (makecard v s) where v is an integer in the range 1 to 10 and s
is a symbol from the set ’hearts, ’diamonds, ’spades and ’clubs;the structure definition is (definestruct
card (value suit)). Create a function maxcard that consumes a nonempty list of cards and produces a
card with the maximum value of any card in the list (if there is more than one, any one will do).
8. Again using the structure card, create a function values that consumes a suit and a list of distinct
cards and produces a list of numbers, that is values of cards from the original list that have the specified
suit. ...
View
Full
Document
This note was uploaded on 10/24/2009 for the course CS 115 taught by Professor Graham during the Fall '08 term at Waterloo.
 Fall '08
 GRAHAM

Click to edit the document details