CS115 - For UWBBS only, no print/modify/copy allowed,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 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 gle@hotmail.com Part 1. Arithmetic Expressionw2009 Part 2. Arithmetic S-Expressionw2009 Part 3. Association Listsw2009 Part 4. Binary Search Treew2009 Part 5. Productsw2009 Part 6. S-Expressionsw2009 Part 7. Taxonsw2009 Part 8. Top-Threesw2009 Part 9. Build-in Functionsw2009 Part 10. Selected Functions and Common Mistakew2009 Part 11. Suggested Lab Exercisesw2009 For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 for detail Part I. Arithmetic Expression (define-struct ae ( fn args ) ) ;; An aexp is a number or (make-ae 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 ( make-ae 'add ( cons ( make-ae 'sub ( cons 5 ( cons 4 empty ) ) ) ( cons ( make-ae 'mul ( cons 3 ( cons 2 empty ) ) ) empty ) ) ) ) (b) ( define as2 ( make-ae 'sub ( cons ( make-ae 'add ( cons 5 ( cons 4 ( cons 2 empty ) ) ) ) ( cons ( make-ae '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, 519-729-1582 for detail Part 2. Arithmetic S-Expression ;; 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, 519-729-1582 for detail Part 3. Association Lists ;;An association list al is empty or (cons (list k v) a-lst) where k is an integer key, v is a ;;string value, and a-lst 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 ) ) ) ) ;; ( get-key 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 ) ) ) ) ;; ( get-val aal ) → ( list “a” “b” “c” ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 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 ) ) ) ) ;; ( get-intersection al1 al2 ) → ( cons ( list 2 “b” ) ( cons ( list 3 “c” ) empty ) ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 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 ) ) ) ) ;; ( get-union 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, 519-729-1582 for detail Part 4. Binary Search Tree (define-struct node ( key val left right ) ) ;; A binary search tree bst is empty or (make-node 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 ( make-tree 50 “” ( make-tree 25 “” ( make-tree 12 “” empty empty ) ( make-tree 37 “” empty empty ) ) ( make-tree 75 “” ( make-tree 57 “” empty empty ) ( make-tree 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 ) → ( make-tree 75 “” ( make-tree 57 “” empty empty ) ;; ( make-tree 85 “” empty empty ) ) ;; ( find bst 1 ) → false 2. implements a function that given a BST and return number of nodes in this tree ;; example: ( num-of-node bst ) → 7 For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 for detail 3. implements a function that given a BST and return a list representing the pre-order iteration ( a pre-order 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 in-order iteration and post-order iteration ( in-order iteration: visit left subtree first, then root, then right subtree ) ( post-order 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, 519-729-1582 for detail Part 5. Products ;; A product-list is either empty or it is ( cons sp pl ), where sp is a sales-product and pl ;; is a product-list (define-struct single-product ( name origin ) ) ;; A single-product is a structure (make-single-product n o), where n is a string, and o is ;; a string denoting the country of origin. (define-struct sales-product ( ID prod ) ) ;; A sales-product is a structure (make-sales-product i p), where i is an integer and p is ;; either a single-product or a product-list. (define plist (list (make-sales-product 102 (make-single-product "p1" "Can")) (make-sales-product 103 (make-single-product "p2" "US")))) (define sp1 (make-sales-product 101 plist)) 1. given a sales-product and a single product, return true if this single product exists in salesproduct, false otherwise. ;; example: ( is-contain-product sp1 ( make-single-product “p1” “Can” ) ) → true ;; example: ( is-contain-product sp1 ( make-single-product “p2” “Chn” ) ) → false; For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 for detail Part 6. S-expressions ;; 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, 519-729-1582 for detail Part 7. Taxons (define-struct t-modern (name pop)) ;; A t-modern is a (make-t-modern n p), where n is a string and p is a number. (define-struct t-ancient (name age left right)) ;; A t-ancient is a (make-t-ancient n a l r), where n is a string, a is a number, and l and r ;; are taxons. ;; A taxon is a t-modern or a t-ancient. ;; ( define t ( make-t-ancient “animal” 500 ( make-t-modern “human” 60 ) ( make-t-modern “cat” 5 ) ) ) 1. write a function that takes a taxon and calculate the total population ;; example: ( total-population t ) → 65 2. write a function that takes a taxon and return a list representing all t-modern ;; example: ( list-of-modern t ) → ( list ( make-t-modern “human” 60 ) ;; ( make-t-modern “cat” 5 ) ) For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 for detail Part 8. Top-Threes (define-struct top-three (name fav1 fav2 fav3)) ;; A top-three is a structure (make-top-three 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 top-three 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 top-three structures and return number of favorite movie in common. ;; ( define p1 ( make-top-three “A” “die hard” “gump” “the core” ) ) ;; ( define p2 ( make-top-three “B” “gump” “simpsons” “the core” ) ) ;; example: ( num-in-common p1 p2 ) → 2 For UWBBS only, no print/modify/copy allowed, solution sold separately, 519-729-1582 for detail Part 9. Build-in Functions map: filter: foldr: reverse: build-list: 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 ) ( build-list 5 even? ) 2. choose which build-in functions to use for the following problem, do not write the function body, some problem may need two or more of given build-in 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( make-card value suit ), output a list of boolean expressions that indicate whether each card is ace ;; example: ( question_c ( list ( make-card 1 'spades ) ( make-card 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, 519-729-1582 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 build-in functions (a) implements 2(d) 2(e) 2(f) and 2(g), no recursion allowed, only build-in 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, 519-729-1582 for detail Part 10. Selected Functions and Common Mistake String-related Functions 1. (string=? s1 s2) produces true if s1 and s2 are character-by-character the same 2. (string-length s) produces the number of characters in s 3. (substring s p1 p2) produces the string containing all the characters from position p1 to p2-1 Other Selected Functions 1. (add1 x) produces x+1 2. (check-expect 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 x-1 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 ) ( string-length “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, 519-729-1582 for detail Part 11. Suggested Lab Exercises Winter 2009 complete the following lab exercises using local and built-in 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 airport-parking 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 film-choice that produces a string indicating the costs of creating a film based on two pricing options. The function film-choice 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 (make-clock 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 (define-struct 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 even-length-number that consumes a list of strings and produces the number of strings in the list that have even length. 5. Create a function switch-case that consumes a string and produces a string in which each lower-case letter is converted to an upper-case letter, each upper-case letter is converted to a lower-case 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 (make-event t d) where t is a symbol (the type of event) and d is a duration in minutes; the structure definition is (define-struct event (type dur)). Create a function total-dur that consumes a list of events and produces the total duration in minutes. 7. Recall the structure card, a structure (make-card 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 (define-struct card (value suit)). Create a function max-card 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

Ask a homework question - tutors are online