CS115_Winter_2009_M2

CS115_Winter_2009_M2 - CS115/116/135/136/2**/3** Private...

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: CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time UWaterloo CS115 Midterm_2 2009 Winter 2009 Oda_Eagle Production CS115/116/125/134/135/136/241/240/341/343 Tutor MSN: odaeagle@hotmail.com Email: eaglediao@gmail.com The following notes are relevant for all questions: • We use the term “Scheme” as a short form for “Beginning Student Scheme with List Abbreviations” • Unless we ask specifically for another part of the design recipe, the instructions “Create a Scheme function” means that you are to give the definition only. • Helper functions may be used anywhere you feel it appropriate • We use arithmetic functions as well as following built-in functions, described briefly here for your reference: ◦ max determines the largest of two or more numbers ◦ min determines the smallest of two or more numbers ◦ sqrt determines the positive square root of a number ◦ string-length determines the length of a string ◦ string-append combines two or more strings into a single string, in the order given ◦ check-expect determines if two values are exactly the same, and is to be used for tests when they are requested ◦ ( make-posn x y ) produces a posn structure with field value x and y ◦ (posn-x p ) and ( posn-y p ) produce the first and second fields of a posn p ◦ ( substring s p1 p2 ) produces a string containing the characters from a from postion p1 through p2 -1 CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Question 1. [ 18 marks ] Boolean and Conditional Expressions a) [ 7 marks ] Use the evaluation rules to perform a full trace the following Scheme program. You may need not all the lines. ( and ( < 3 4 ) ( or ( = 3 5 ) ( = 3 3 ) ( > 3 5 ) ) ) b) [ 3 marks ] The following conditional statement generates an error. Explain in a single sentence what the problem is. ( cond [ ( = 10 9 ) ( / 34 0 ) ] [(>18)(*23)] [ ( < 2 1 ) “ABC”] ) CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time c) [ 4 marks ] Which pairs of the following predicates are equivalent( i.e. would produce the same values for all integer values of x ) ? Briefly justify your answer. A: ( define ( cond 1 x ) ( and ( < x 50 ) ( < 0 x ) ) ) B: ( define ( cond2 x ) ( cond [ ( < 0 x ) true ] [ ( < x 50 ) true ] [ else false ] ) ) C: ( define ( cond3 x ) ( or ( < x 50 ) ( < 0 x ) ) ) D: ( define ( cond4 x ) ( cond [ ( and ( < 0 x ) ( < x 50 ) ) true ] [ else false ] ) ) Circle all pairs that are equivalent: ( A and B ) ( A and C ) ( A and D ) ( B and C ) ( B and D ) ( C and D ) d) [ 4 marks ] Complete the following Scheme predicate, which could be used if check-expect was not available. ;; check-answer: any any → boolean ;; produces true if actual and expected are equal, and false otherwise ;; example: ( check-answer “ABC” 123 ) → false ;; example: ( check-answer 123 123 ) → true ( define ( check-answer actual expected ) CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Question 2. [ 10 marks ] Writing a Conditional Function Complete the function weather-message that produces a string describing the weather, as determined by a given temperature( according to the table below ) and a string describing current conditions ( e.g. “snowing”, “sunny”, etc. ). The String produced should follow the pattern given in the examples. For full marks, do not use any compound Boolean expressions in the questions of your conditional expressions( i.e. for full marks, do not use and and or ). ;;weather-message: num string → string ;;produces a string combining a temperature message with another string ;;describing current conditions. The message for the tempeSrature is: ;; Temperature t Temperature message ;; t<0 “cold” ;; 0 <= t < 10 “cool” ;; 10 <= t < 20 “warm” ;; 20 <= t “hot” ;; examples: ( weather-message 15 “raining” ) → “ warm and raining” ;; ( weather-message -10 “sunny” ) → “cold and sunny” ( define ( weather-message t current ) Question 3. [ 22 marks ] Using Structures CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Recall the structure bill from assignment 3: ;; A bill is a structure ( make-bill n c ) where n is a string ( for a person's name ) and ;; c is a non-negative number ( for the outstanding charge ) ( define-struct bill ( name charge ) ) a) [ 2 marks ] Define a constant my-bill, of type bill, for “Li” with a charge of 9.99 b) [ 10 marks ] Create a function owes-more that consumes two bills and produces the name of the person with the higher charge on their bill. If both people owe the same amount, the function can produce either name. ;; owes-more: bill bill → string ;; produces the name of the person with the higher charge ;; example: ( owes-more ( make-bill “A” 10.65 ) ( make-bill “B” 8.21 ) ) → “A” ( define ( owes-more bill1 bill2 ) c) [ 10 marks ] Create a function reduce-bill which consumes a bill and a non-negative integer, and CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time produces a new bill for the same person with the charge reduced by the indicated amount. If the consumed number is larger than the charge owing, then the new bill should have a charge of 0. ;; reduce-bill : bill num → bill ;; produces a new bill with the charge reduced by the specified amount ;; if the payment exceeds the amount owing, the charge is set to 0. ;; example: ( reduce-bill ( make-bill “A” 10.99 ) 8.24 ) ) → ( make-bill “A” 2.75 ) ;; example: ( reduce-bill ( make-bill “A” 10.99 ) 12.00 ) ) → ( make-bill “A” 0 ) ( define ( reduce-bill a-bill amt ) Question 4. [ 10 marks ] Understanding Lists CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Answer questions (a)-(d) about the following list. ( define mylist-1 ( cons 45 ( cons “hello” ( cons true ( cons 59 empty ) ) ) ) ) a) [ 1 mark ] What is the value of ( length mylist-1 )? b) [ 1 mark ] What is the value of ( rest ( rest mylist-1 ) )? c) [ 1 mark ] What is the result of ( rest ( rest ( rest ( rest ( rest mylist-1 ) ) ) ) ) ? d) [ 3 marks ] Write a Scheme expression to add the two numbers in mylist-1, your solution must make reference to mylist-1 and use the functions first and rest. Answer questions (e)-(f) about the following list. ( define mylist-2 ( list ( make-posn 1 2 ) ( make-posn 3 4 ) ( make-posn 5 6 ) ) ) e) [ 1 mark ] What is the value of ( first ( rest mylist-2 ) ) ? f) [ 3 marks] Write a Scheme expression to add together the x and y fields of the first posn in mylist-2. Your solution must make reference to mylist-2 and use the posn selector functions, as well as the function first. Question 5. [ 30 marks ] Basic List Functions CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time These questions all deal with a list of string values. a) [ 10 marks ] Create a function count-start-Aa which produces the number of strings in a list of nonempty strings which begin with either “A” or “a”. ;; count-start-Aa: ( list of string ) → num ;; produces the number of strings, in los which begin with either “A” or “a”. ;; example: ( count-start-Aa ) ( list “aBc” “bdf” “Abaaa” ) ) → 2 ( define ( count-start-Aa los ) b) [10 marks ] Create a function lengths which consumes a list of strings and produces a list of numbers, of the same length, containing the lengths, in order, of each string in the original list. ;; lengths: ( list of string ) → ( list of num ) ;; produces list of numbers which contains the lengths of all strings in los ;; example: ( lengths ( list “a” “” “ABC-123” ) ) → ( list 1 0 7 ) ( define ( lengths los ) c) [ 10 marks ] Create a function same-start-end which consumes a list of strings and produces a list of CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time strings from it containing only those strings in which the first and last characters are the same. Note that any empty strings in the consumed list are not to be included in the produced list, but strings containing single character should be included. ;; same-start-end : ( list of string ) → ( list of string ) ;; produces the list of all strings in a los in which the first and last ;; characters are the same ( define ( same-start-end los ) Question 6. [ 10 marks ] List of Structures CS115/116/135/136/2**/3** Private Tutorial Services MSN: odaeagle@hotmail.com E-mail: eaglediao@gmail.com Please Email me for more information such as price and available time Recall the structure clock from lab 5: 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 ( define-struct clock ( hours minutes ) ) Create a function earliest which consumes a list of clock structures and produces the clock value which occurs the earliest in the day, you may assume that the consumed list is non-empty. ;;earliest: ( list of clock ) [non-empty] → clock ;; produces the clock value in the given non-empty list of ;; clock values ( corresponding to the earliest time in the list ;; example: ( earliest ( list ( make-clock 13 45 ) ( make-clock 8 52 ) ( make-clock 23 00 ) ) ) ;; → ( make-clock 8 52 ) ( define ( earliest loc ) ...
View Full Document

Ask a homework question - tutors are online