W2009 Midterm 2

W2009 Midterm 2 - UWaterloo CS116 Midterm_2 2009 Winter...

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: UWaterloo CS116 Midterm_2 2009 Winter 2009 Oda_Eagle Production CS115/116/125/134/135/136/241/240/341/343 Tutor odaeagle@hotmail.com The following notes are relevant for all questions: • 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 招收 CS 大三課程以下学生有意者(具體請見詳表) + odaeagle@hotmail.com CELL: 519-729-1582 已经教过 100+的学生 ◦ 经验丰富,童叟无欺,价格合理,内容充实,简单易懂 具體詳細表 CS115 作業答疑,考試複習,有過去考試的卷子和過去所有作業準確答案,也有自出的題,價格面儀 CS116/136 普通作業答疑不收費,如果一對一輔導價格面議 CS134 擁有最全的資料,無論作業還是近四個 term 所有考試卷子(不包括 final),價格面儀 CS240 答疑解惑,不收費 CS241 答疑+Debug 需要請請吃飯 CS246 答疑解惑 免費 ,Debug 需要請吃飯 CS245/251 愛莫能助 CS341 答疑解惑 不收費 CS350 答疑解惑 + Debug 不收費 CS343 答疑解惑 + Debug 不收費 CS360 跟我討論問題,我請你吃飯 其他課程諸如 Math135/136/137/138,可以給你介紹好老師 想學 Flash 的同學也可以找我,不過價格會比 CS 課貴 想學唱歌的別找我,謝謝 喝酒的找我,隨時奉陪 1. [ 7 marks total ] (a) [ 4 marks ] Suppose the following Scheme expressions have been entered into the Interactions window in DrScheme, in the order that appears below. Using the memory model shown in class, draw a picture showing the assignment of values to variables. ( define num1 4 ) ( define num2 -7 ) ( define num3 num1 ) ( define pt1 ( make-posn 3 0 ) ) ( define pt2 ( make-posn 3 0 ) ) ( define pt3 pt1 ) (b) [ 3 marks ] The expressions from part (a) are repeated here for your convenience ( define num1 4 ) ( define num2 -7 ) ( define num3 num1 ) ( define pt1 ( make-posn 3 0 ) ) ( define pt2 ( make-posn 3 0 ) ) ( define pt3 pt1 ) Suppose the following expressions are entered into the Interactions window after expressions entered in part (a). For each expression followed by an empty box, fill the box with what the expression produces. The first box has been filled in for you. num1 ( set! Num1 8 ) num1 num3 ( set-posn-x! pt1 1 ) ( posn-x pt1 ) ( posn-x pt2 ) ( set-posn-y! pt2 num2 ) ( set! num2 6 ) ( posn-y pt2 ) ( set! pt3 ( make-posn 3 0 ) ) ( = ( posn-x pt1 ) ( posn-x pt3 ) ) 4 2. [ 7 marks ] Use accumulative recursion to write a function add-digits that consumes a natural number n and produces the sum of the digits of that number. Your solution must run in time which is linear in the number of digits of n. Contract: ;; add-digits: nat → nat Examples: ;; ( add-digits 427 ) → 13 ;; ( add-digits 12 ) → 3 ;; ( add-digits 0 ) → 0 Body: 3. [ 3 marks ] For each of the following Scheme functions identify the worst-case running time as one of • • • Linear ( denoted as O(n) ) quadratic ( denoted as O( n2 ) ) or exponential ( denoted as O( 2n ) ) (a) Choose the worst-case running time of funct-A. ( define ( funct-A mylist ) ( local ( define ( helper al ) ( cond [ ( empty? Al ) 0 ] [ else ( + ( first al ) ( helper ( rest al ) ) ] ) ) ) ) ( cond [ ( empty? mylist ) empty ] [ else ( cons ( helper mylist ) ( funct-A ( rest mylist ) ) ) ) ) ) (b) Choose the worst-case running of funct-B. ( define ( funct-B mylist ) ( cond [ ( empty? ( rest mylist ) ) empty ] [ else ( cons ( first mylist ) ( funct-B ( rest mylist ) ) ] ) ) ) ) (c) Choose the worst-case running of funct-C. ( define ( funct-C mylist ) ( cond [ ( empty? ( rest mylist ) ) ( first mylist ) ] [ ( < ( first mylist ) ( funct-C ( rest mylist ) ) ) ( first mylist ) ] [ else ( funct-C ( rest mylist ) ) ] ) ) 4. [ 7 marks ] Use accumulative recursion to write a function smallest-sum which consumes a list of natural numbers ( where there are at least 2 numbers in the list ) and produces the smallest pair-wise sum. By “pair-wise” sum, we mean the sum of two adjacent elements in the list. As an example, consider the list ( list 1 4 2 3 8 ) : there are 4 pair-wise sums. ( 1 + 4 = 5 , 4 + 2 = 6 , 2 + 3 = 5 and 3 + 8 = 11 ). In this case, the smallest pair-wise sum would be 5 ( which happens to occur twice ). Contract: ;; smallest-sum ( list of nat ) [ length >= 2 ] → nat Examples: ;; ( smallest-sum '(1 2 3 4 5 ) ) → 3 ;; ( smallest-sum '(1 4 2 5 1 8 1 ) → 5 ;; ( smallest-sum '( 100 100 ) ) → 200 Body: 5. [ 7 marks ] Suppose you have the following state variables defined: ( define x 1 ) ( define y 1 ) Write a Scheme function funct-mutation that consumes two functions f1 and f2, each of which consume two elements ( both of the same type ) and produce one element ( which has the same type as the consumed type ). The function funct-mutation should alter x to the result of applying f1 to x and y. and it should alter y to the result of applying f2 to x and y. Contract: Examples: ;; if x has the value 1 and y has the value of 1, and we evaluate ;; ( funct-mutation + quotient ) , then x has the value 2 and ;; y has the value 1. Body: Tests ( provide one using values different from the example above ): 6. [ 3 marks ] Consider the code: ;; mystery: ( list of nat( >= 1 ) ) → nat ( define ( mystery alopn ) ( local ;; strip-k: nat ( list of nat ( >=1 ) ) → ( list of nat ( >=1 ) ) ;; Purpose: strip-k removes the first k elements from the provided list; if there are k or ;; fewer elements in the list, the empty list is returned ;; Examples: ( strip-k 0 ( list 1 2 3 ) ) → ( list 1 2 3 ) ;; ( strip-k 2 ( list 1 2 3 ) ) → ( list 3 ) ( define ( strip-k k lst ) ( cond ( ( zero? K ) lst ) ( ( empty? Lst ) empty ) ( else ( strip-k ( sub1 k ) ( rest lst ) ) ) ) ) ) ( cond ( ( empty? Alopn ) 0 ) ( else ( add1 ( mystery ( strip-k ( first alopn ) alopn ) ) ) ) ) ) for each of the following test cases, give the output of mystery (a) ( mystery empty ) → (b) ( mystery ( list 1 10 9 8 ) ) → 2 (c) ( mystery ( list 1 2 10 3 1 1 1 ) ) → 4 ...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Ask a homework question - tutors are online