Offical+Review+Quesiton+W2009

Offical+Review+Quesiton+W2009 - CS115 Final Exam Review...

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 Final Exam Review Session Solution la st upd ate A pr 19 th, 2009 c o py ri ght 2009 O d a_E a gle P rodu ction, all right re s erved C S 1 15/116/125/134/135/136/241/240/341/343 Tutor od a e a gle@hot m ail.co m 1. Write a recursive function sum-powers-one-half, which consumes a natural number n and computes 1+ ½ + ½2 + … + (½)n-1 + (½)n ( define ( sum-powers-one-half n) ( cond ( ( zero? n ) 1 ) ( else ( + ( expt 1/2 n ) ( sum-powers-one-half ( sub1 n ) ) ) ) ) ) 2. Write a function to return the largest key in a non-empty association list. ( define ( largest-key aal ) ( local ( ( define ( largest-key-help aal msf) ( cond ( ( empty? aal ) msf ) ( ( < ( first ( first aal ) ) ( first msf ) ) ( largest-key-help ( rest aal ) msf ) ) ( else ( largest-key-help ( rest aal ) ( first aal ) ) ) ) ) ) ( cond ( ( empty? aal ) empty ) ( else ( largest-key-help aal ( first aal ) ) ) ) ) ) 3. bsts can be used to store dictionaries as well. Write a function to return the largest key stored in a bst. Note: make use of the property of the bst. ( define ( largest-key-bst bst ) ( cond ( ( empty? ( tree-right bst ) ) ( tree-key bst ) ) ( else ( largest-key-bst ( tree-right bst ) ) ) ) ) 4. Consider finding the longest string which is stored as an associated value in a dictionary. Write this method for both implementations of dictionaries discussed in class. Almost as same as 2. and 3. instead of comparing keys, comparing value this time 5. write a a function is-bst? Which consume a value claiming to be a bst, and produce true if all conditions to be a bst. ( define ( isBST t ) ( local ( ( define ( checkChild choose t ) ( cond ( ( empty? ( choose t ) ) true ) ( else ( isBST ( choose t ) ) ) ) ) ( define ( check-root t ) ( cond ( ( empty? ( tree-left t ) ) ( < ( tree-key t ) ( tree-key ( tree-right t ) ) ) ) ( else ( < ( tree-key ( tree-left t ) ) ( tree-key t ) ) ) ) ) ) ( cond ( ( and ( empty? ( tree-left t ) ) ( empty? ( tree-right t ) ) ) true ) ( else ( and ( check-root t ) ( and ( checkChild tree-left t ) ( checkChild tree-right t ) ) ) ) ) ) ) 6. Write a function which returns true if all keys provided bst are prime value. ( define ( is-condition? ACond choice bst ) ( cond ( ( empty? bst ) true ) ( ( ACond ( choice bst ) ) ( and ( is-condition? ACond choice ( tree-left bst ) ) ( is-condition? ACond choice ( tree-right bst ) ) ) ) ( else false ) ) ) ;; note, this is actually a abstract function, not exacly what question 6 want ;; use it as ( is-condition? prime? tree-key bst ) 7. Write a function which consumes a bst and returns a list of all the keys in the tree which are prime numbers. ( define ( keep-condition ACond choice bst ) ( cond ( ( empty? bst ) empty ) ( ( ACond ( choice bst ) ) ( append ( cons ( choice bst ) empty ) ( keep-condition ACond choice ( tree-left bst ) ) ( keep-condition ACond choice ( tree-right bst ) ) ) ) ( else ( append ( keep-condition ACond choice ( tree-left bst ) ) ( keep-condition ACond choice ( tree-right bst ) ) ) ) ) ) ;; this is an abstract function too ;; use it as ( keep-condition prime? tree-key bst ) 8. Write a function which compares the two lists of strings pairwise, and produces true if first string in list1comes before the first string in list2, and similarly the second string, and third, etc. You may assume that the lists have the same length. ( define ( compare-two-list ACond list1 list2 ) ( cond ( ( empty? list1 ) true ) ( ( ACond ( first list1 ) ( first list2 ) ) ( compare-two-list ACond ( rest list1 ) ( rest list2 ) ) ) ( else false ) ) ) ;; this is still an abstract function( two list at same speed ) 9. Write a fn which consumes two lists of strings, and produces true if all the strings in the first list come before all the strings in the second list, and produces false otherwise. Assume both lists are arranged in alphabetical order. ( define ( compare-two-list-diff ACond list1 list2 ) ( local ( ( define ( check-help ACond item list2 ) ( cond ( ( empty? list2 ) true ) ( ( ACond item ( first list2 ) ) ( check-help ACond item ( rest list2 ) ) ) ( else false ) ) ) ) ( cond ( ( empty? list1 ) true ) ( ( check-help ACond ( first list1 ) list2 ) ( compare-two-list-diff ACond ( rest list1 ) list2 ) ) ( else false ) ) ) ) 10. Write a function that consumes a mixed list of movies and photos, and produces a list of all the movies in the list. ( filter movie? lom ) 11. Write a function that consumes a list of photos and a percentage (between 0 and 100) and produces a new list of photos in which each photo is enlarged by the specified amount. ( map enlarge lom ) 12.Write a function that consumes a mixed list of photos and movies, and the number of seconds to display single photo, and produces the total length in seconds of the presentation. (Assignment 5, Question 1) (define (get-presentation-length lom photo-dur) ( cond ( ( empty? Lom ) 0 ) ( ( photo? ( first lom ) ) ( + photo-dur ( get-presentation-length ( rest lom ) photo-dur ) ) ) ( ( movie? ( first lom ) ) ( + ( movie-duration ( first lom ) ) ( get-presentation-length ( rest lom ) photo-dur ) ) ) ( else ( get-presentation-length ( rest lom ) photo-dur ) ) ) ) ...
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.

Ask a homework question - tutors are online