CS 135 Fall 2018 Final Exam Review CS 135 Fall 2018 Final Exam Review 1
Final Exam Information The final exam will be held on Saturday, December 15 th 9:00AM - 11:30AM in the PAC Check your exam seating assignment on Odyssey. CS 135 Fall 2018 Final Exam Review 2
ALFs - digits->num Without using explicit recursion, write a function called digits->num that consumes a non-empty list of digits and produces the number those digits form. Include a contract. Try to do it without reversing the list. (digits->num (5)) 5 (digits->num (1 7 4 9 4)) 17494 CS 135 Fall 2018 Final Exam Review 3
ALFs - count Without using explicit recursion, write a function called count that consumes 2 arguments, of which the second one is a list. It produces the number of times the first argument occurs in the list. (count cat (3 "cat" 11)) 0 (count cat ("cat" cat 4 cat 6)) 2 CS 135 Fall 2018 Final Exam Review 4
ALFs - count-chars Without using explicit recursion, write a function called count-chars that consumes a Char and a (listof Any). It should count the number of occurrences of the character in all the strings in the list. You may use count that you wrote in the previous question. (count-chars #\a (a "Aardvark" #\a ("abba" 12) "hagrid!?")) 3 CS 135 Fall 2018 Final Exam Review 5
Nested List - count-chars/nested Write a function called count-chars/nested that consumes a Char and a Nest-List-Any. It should count the number of occurrences of the character in all the strings in the nested list. You can use explicit recursion. You may also use count that you wrote previously. (count-chars/nested #\a (a "Aardvark" (#\a "hag" "ah") "at" ("doggo"))) 5 Recall the data definition for a nested list: ;; A (Nest-List-Any) is one of: ;; * empty ;; * (cons Any Nest-List-Any) ;; * (cons Nest-List-Any Nest-List-Any) CS 135 Fall 2018 Final Exam Review 6
Graphs - delete-node Write a function delete-node that consumes a Node v and a Graph g and produces the graph with v removed. If v does not exist in g, the same graph is produced. (define g1 ((A (B C F)) (B ()) (C (A B)) (D (A C F)) (E (A B C)) (F (A))) (define g1 ((A (B C F)) (B ()) (C (A B)) (D (A C F)) (E (A B C)) (F (A))) (delete-node C g1) ((A (B F)) (B ()) (D (A F)) (E (A B)) (F (A))) CS 135 Fall 2018 Final Exam Review 7
Stepping Lambda: Example Given that the following definition has been processed in the Intermediate Student with Lambda language: (define fun (lambda (x y) (lambda (y z) (lambda (z) (+ x y z))))) Produce a step-by-step evaluation of the following program: (((fun 1 2) 3 4) 5) CS 135 Fall 2018 Final Exam Review 8
Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) CS 135 Fall 2018 Final Exam Review 9
Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) CS 135 Fall 2018 Final Exam Review 10
Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) CS 135 Fall 2018 Final Exam Review 11
Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) (+ 1 3 5) CS 135 Fall 2018 Final Exam Review 12
Stepping Lambda: Example ((((lambda (x y) (lambda (y z) (lambda (z) (+ x y z)))) 1 2) 3 4) 5) (((lambda (y z) (lambda (z) (+ 1 y z))) 3 4) 5) ((lambda (z) (+ 1 3 z)) 5) (+ 1 3 5) 9 CS 135 Fall 2018 Final Exam Review 13
BTs ;; An Integer Binary Tree (IntBT) is one of: ;; * empty ;; * an IntNode (define-struct int-node (key left right)) ;; An IntNode is a (make-int-node Int IntBT IntBT) Note this is different from a BST! CS 135 Fall 2018 Final Exam Review 14
BTs - min-key Write a function, min-key that consumes a non-empty IntBT and produces the minimum key in that tree. 3 (define b1 (make-int-node 3 900 (make-int-node 900 empty (make-int-node 200 empty empty)) (make-int-node 6 (make-int-node 8 empty empty) (make-int-node 10 (make-int-node 2 empty empty) empty)))) (min-key b1) 2 200 6 8 10 2 CS 135 Fall 2018 Final Exam Review 15
Which of the following was one of Grace Hopper s achievements? A) Proof that the Halting Problem is undecidable B) Design for the Analytical Engine C) Creation of the first compiler D) Axiomatization of geometry E) Development of FORTRAN CS 135 Fall 2018 Final Exam Review 16
Mutual recursion - Extended-Factor-Tree For this example, we are going to build an extended factor tree for a positive natural number. The extended factor tree of a number n has the number itself as the root node and all of its factors between 2 and n-1, in the extended factor tree form. Write build-tree that consumes a natural number n and produces the extended factor tree form of n. ;; An extended-factor-tree is a (list Nat (listof extended-factor-tree)) 12 (define sample-tree (list 12 (list (list 2 empty) (list 3 empty) (list 4 (list (list 2 empty))) (list 6 (list (list 2 empty) (list 3 empty)))))) 2 3 4 2 6 2 3 CS 135 Fall 2018 Final Exam Review 17
Who developed the Lisp programming language? A) John von Neumann B) Alonzo Church C) Kurt Gödel D) Grace Hopper E) John McCarthy CS 135 Fall 2018 Final Exam Review 18
Generative Recursion: group-equal-elems Write a function group-equal-elems that consumes an arbitrary list lst and rearranges the elements in lst so that identical elements are all grouped together. The rearranged elements should appear in the the same relative order as the first unique occurrence of each value in the consumed lst when read from left to right. (group-equal-elems (list 3 4 6 0 6 4 5 3 5 6 6 3 0 0 3 6 4 5 6 5)) (list 3 3 3 3 4 4 4 6 6 6 6 6 6 0 0 0 5 5 5 5) CS 135 Fall 2018 Final Exam Review 19
Generative Recursion: group-equal-elems ;; group-equal-elems: (listof X) (listof X) (define (group-equal-elems lst) (cond [(empty? lst) empty] [else (local [(define matches (filter (lambda (x) (equal? x (first lst))) lst)) (define others (filter (lambda (x) (not (equal? x (first lst)))) lst))] (append matches (group-equal-elems others)))])) CS 135 Fall 2018 Final Exam Review 20
What question do you want next? A) Abstract List Function Problems B) Graph Problems C) Tree Problems D) Mutual Recursion Problems E) Other Problems CS 135 Fall 2018 Final Exam Review 21
Generative recursion - count-uniq Write a function count-uniq that consumes a list and produces the number of unique elements in it. Do not use an accumulator. (count-uniq (a a 1 b 1 2)) 4 (count-uniq (a b c)) 3 (count-uniq ()) 0 CS 135 Fall 2018 Final Exam Review 22
ALFs: take-last Write a function take-last that consumes a list and a natural number n, and produces a list containing only the last n elements of the list, or the entire list if it contains fewer than n elements. (take-last empty 1) empty (take-last (b a s e) 2) (s e) (take-last (list 1 1 2) 4) (list 1 1 2) CS 135 Fall 2018 Final Exam Review 23
ALFs: take-last ;; take-last: (listof X) Nat (listof X) (define (take-last lst n) (foldr (lambda (itm rr) (cond [(= n (length rr)) rr] [else (cons itm rr)])) empty lst)) CS 135 Fall 2018 Final Exam Review 24
ALFs: take-last The previous solution calls length over and over again, which makes it very inefficient. Here are some additional approaches to the problem. As an exercise, you should try implementing them in racket: Use foldr to go over the list starting from the back. We keep extra info in our recursive result to tell us how long the current list is. This way, we don t need to compute length over and over again. Reverse the list, take the first n elements, then reverse our answer. We only call reverse twice in this solution. Zip each element with an index (0 referring to the last element), then we cut the list from the front until the index is smaller or equal to n. In the end, remove the index with a map. CS 135 Fall 2018 Final Exam Review 25
Length of a Longest Path Recall from lectures that a sequence of nodes v 1, v 2,..., v k is a path of length k 1 if (v 1, v 2 ), (v 2, v 3 ),..., (v k 1, v k ) are all edges. Directed graphs without cycles are called directed acyclic graphs (DAGs). Write a function max-path-length that consumes a DAG dag and produces the length of a longest path in dag. If dag has no paths, produce 0. CS 135 Fall 2018 Final Exam Review 26
;; max-path-length/list: (listof Node) Graph Nat (define (max-path-length/list l-nds dag) (cond [(empty? l-nds) 0] [else (local [(define nbrs (neighbours (first l-nds) dag))] (cond [(empty? nbrs) (max-path-length/list (rest l-nds) dag)] [else (max (add1 (max-path-length/list nbrs dag)) (max-path-length/list (rest l-nds) dag))]))])) ;; max-path-length: Graph Nat (define (max-path-length dag) (max-path-length/list (map first dag) dag)) CS 135 Fall 2018 Final Exam Review 27
BSTs - keys-in-range Write a function, keys-in-range that consumes 2 numbers, lo and hi, and a BST. It produces a list of all the keys in the BST that are between lo and hi inclusive. You should not try to convert the entire tree into a list first. garbage 3 (keys-in-range 2 7 empty) empty (keys-in-range 2 7 b1) (2 3 4 6) 1 6 (keys-in-range 5 2 b1) empty 2 4 10 8 CS 135 Fall 2018 Final Exam Review 28
Stepping Local When renaming local definitions append 0 if possible, or else 1, 2, etc. Do not recopy any line that is already in simplest form. (define x 5) (define y 10) (define (my-fn a b) (local [(define x (+ a b)) (define y (+ x (local [(define x 20)] x)))] (list x y))) (my-fn x y) CS 135 Fall 2018 Final Exam Review 29
ALFs - build-x Write build-x that consumes an odd natural number n and produces a table as a (listof (listof Nat)) that contains 1s on the 2 diagonals and 0s everywhere else. Do not use explicit recursion. (ones-on-diagonal 1) (list (list 1)) (ones-on-diagonal 3) (list (list 1 0 1) (list 0 1 0) (list 1 0 1))) CS 135 Fall 2018 Final Exam Review 30
Mutual recursion - Tournament For this example, we are going to use trees to model the results of a tournament. (define-struct tournament (winner round)) ;; A Tournament is a (make-tournament Str Round) ;; requires: winner is the name of the player with the best time ;; in the topmost round ;; A Round is a (listof Player) (define-struct player (name time last-round)) ;; A Player is a (make-player Str Num Round) ;; requires: time > 0 ;; name is the player with the best time in the topmost ;; round of last-round if it is non-empty CS 135 Fall 2018 Final Exam Review 31
Jesse Ben 12.3 Bill 16.9 Jesse 12.1 Anne 13.1 Ben 11 Bill 15.1 Yao 15.8 Jason 18.3 Jesse 19.9 Jimmy 9999 (define cs135-cup (make-tournament "Jesse" (list (make-player "Ben" 12.3 (list (make-player "Anne" 13.1 empty) (make-player "Ben" 11 empty))) (make-player "Bill" 16.9 (list (make-player "Bill" 15.1 empty) (make-player "Yao" 15.8 empty) (make-player "Jason" 18.3 empty))) (make-player "Jesse" 12.1 (list (make-player "Jesse" 19.9 empty) (make-player "Jimmy" 9999 empty)))))) CS 135 Fall 2018 Final Exam Review 32
Mutual recursion - Templates Write a template function for each of Tournament, Round and Player. (define-struct tournament (winner round)) ;; A Tournament is a (make-tournament Str Round) ;; requires: winner is the name of the player with the best time ;; in the topmost round ;; A Round is a (listof Player) (define-struct player (name time last-round)) ;; A Player is a (make-player Str Num Round) ;; requires: time > 0 ;; name is the player with the best time in the topmost ;; round of last-round if it is non-empty CS 135 Fall 2018 Final Exam Review 33
Mutual recursion - new-record Here s a structure we will use to store information about a record: (define-struct record (name time)) ;; A Record is a (make-record Str Num) ;; requires: time > 0 A player makes a new record if they have a faster time than the current record. Using your templates, write a function new-record that consumes a current record and a Tournament. It should produce a Record structure with the name and time of the player in the tournament who has the new record. If no one in the tournament had a faster time than the current record, produce false. You may assume that all times in the tournament are unique. (new-record (make-record "Bill" 11.2) cs135-cup) (make-record "Ben" 11) (new-record (make-record "Karen" 3.1) cs135-cup) false CS 135 Fall 2018 Final Exam Review 34
Stepping Lambda Recall that the rule for evaluating anonymous lambda expression is ((lambda (x1... xn) exp) v1... vn) exp where exp is exp with all occurrences of x1 replaced by v1, all occurrences of x2 replaced by v2, and so on. CS 135 Fall 2018 Final Exam Review 35
Stepping Local An expression of the form (local [(define x1 exp1)... (define xn expn)] bodyexp) is handled as follows. x1 is replaced with a fresh identifier (call it x1 new) everywhere it s used in the local expression. The same thing is done with x2 through xn. The definitions (define x1 new exp1)... (define xn new expn) are then lifted out (all at once) to the top level of the program, preserving their order. CS 135 Fall 2018 Final Exam Review 36
Stepping Local When all the rewritten definitions have been lifted out, what remains looks like (local [] bodyexp ), where bodyexp is the rewritten version of bodyexp. This is just replaced with bodyexp. All of this is a single step. (local [(define x1 exp1)... (define xn expn)] bodyexp) (define x1 0 exp1 )... (define xn 0 expn ) bodyexp CS 135 Fall 2018 Final Exam Review 37
Accumulative and Generative Recursion Accumulative Recursion All parameters in the recursive call are either unchanged or one step closer to the base case (as with pure structural recursion), plus one or more parameters that accumulate partial answers Accumulators are usually produced in the base case(s) directly, or manipulated before being produced In the recursive call, it does not matter how complicated the code is to update an accumulator, as long as it adds on to the partial answer CS 135 Fall 2018 Final Exam Review 38
Generative Recursion A parameter is considered generative if: It is changed in the recursive call without a clear base case It doesn t get one step closer to the base case in the recursive call, according to the data definition It is not an accumulator, and thus isn t produced in the base case Examples of generative recursive calls: (collatz (+ 1 ( 3 n))) (foo (not bool)) (foo (rest (rest (rest lst)))) (foo (remove elem lst)) If in any recursive call of a function, there is at least one parameter that satisfies the conditions above, regardless of how the other parameters are changing, the function uses generative recursion. CS 135 Fall 2018 Final Exam Review 39