Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS116 – Tutorial 3 Accumulative Recursion/Runtime.

Similar presentations


Presentation on theme: "CS116 – Tutorial 3 Accumulative Recursion/Runtime."— Presentation transcript:

1 CS116 – Tutorial 3 Accumulative Recursion/Runtime

2 Reminders: Assignment 3 is due Wednesday at 10 am.

3 Review Accumulative Recursion Run Time

4 Accumulative Recursion (define (fn lst) (local [(define (acc-fn whats-left acc1 acc2 … accn) (cond [(base-case whats-left)…acck…] … [else (acc-fn update-parameters)]))] (acc-fn initial-whats-left initial-acck … )) Accumulators “keep track” of something so that you can quickly produce the expected result Accumulators: can have as many as you need Usually use in place of lst when recursing Use accs to help produce the correct result Put more cases in if you need them

5 Question 1 Write an accumulatively recursive function sum-list which consumes a list of numbers and produces their sum. Trace (sum-list (list 1 2 3)) Compare this accumulative version with the structural recursive version from earlier.

6 Trace Comparison Without Accumulative Recursion: (sum-list (list 1 2 3)) => (+ 1 (sum-list (list 2 3))) => (+ 1 (+ 2 (sum-list (list 3)))) => (+ 1 (+ 2 (+ 3 (sum-list empty)))) => (+ 1 (+ 2 (+ 3 0))) => (+ 1 (+ 2 3)) => (+ 1 5) => 6

7 Trace Comparison With Accumulative Recursion (sum-list (list 1 2 3)) => (sum-list-acc (list 1 2 3) 0) => (sum-list-acc (list 2 3) (+ 1 0)) => (sum-list-acc (list 2 3) 1) => (sum-list-acc (list 3) (+ 2 1)) => (sum-list-acc (list 3) 3) => (sum-list-acc empty (+ 3 3)) => (sum-list-acc empty 6) => 6

8 Question 2 Develop an accumulatively recursive function list-to-num that consumes a nonempty list, digits,of numbers between 0 and 9, and returns the number corresponding to digits. For example, (list-to-num (list 9 0 8)) should return 908, while (list-to-num (list 8 6)) should return 86. Trace the application (list-to-num (list 8 0 2))

9 Trace of list-to-num (list-to-num ‘(8 0 2)) ⇒ (list-to-num-acc ‘(8 0 2) 0) ⇒ (list-to-num-acc ‘(0 2) (+ (* 10 0) 8)) ⇒ (list-to-num-acc ‘(0 2) 8) ⇒ (list-to-num-acc ‘(2) (+ (* 10 8) 0)) ⇒ (list-to-num-acc ‘(2) 80) ⇒ (list-to-num-acc empty (+ (* 10 80) 2)) ⇒ (list-to-num-acc empty 802) ⇒ 802

10 Question 3 Write an accumulatively recursive function find that consumes a list of symbols alos and a symbol sym, and returns the list of indices of positions in alos with symbol sym. Recall that the first position in a list has index 0. You may use reverse. For example, (find (list ‘a ‘v ‘d ‘v) 'v) should return (list 1 3), while (find (list ‘ a ‘ v ‘ d ‘ v) 'q) should return empty.

11 Question 4 Write an accumulatively recursive function count-max that consumes a nonempty list of numbers alon and returns the number of times the largest number in alon appears. For example, ◦ (count-max (list 1 3 5 4 2 3 3 3 5)) => 2 ◦ since the largest element of the list, 5, appears twice. Your function should pass through the list only once.

12 Runtime Review Look at the “worst case” scenario (i.e. longest time) Only for code that gets executed when you run it Assume function works (i.e. will not produce an error when you run it)

13 Runtime Review O(1) – Constant ◦ does not depend on the size of the input (first x), (rest x), (symbol? x), (map sqr (list 1 2 3)) Ex: simple functions, or does not do something n times O(n) – Linear ◦ depends on the size of the input (map sqr (list1 2 … n)) Ex: abstract list functions, function call once (on a single cond case)

14 Runtime Review O(n 2 ) – Quadratic ◦ time proportional to square of input (map sqr (build-list n add1)) Ex: nested abstract list function, O(n) done multiple times O(2 n ) – Exponential ◦ As size of input increases, run time doubles Module 3, Slide 22: fib Ex: Multiple function calls (>1 calls in one cond case)

15 Question 5 For each of the following determine what the worst-case runtime is.

16 Determine the worst-case run-time in terms of n, assuming n elements in lon (define (sum-list1 lon) (cond [(empty? lon) 0] [else (+ (first lon) (sum-list1 (rest lon)))]))

17 Determine worst-case run-time in terms of n, assuming n elements in loi (define (evens loi) (filter even? loi))

18 Determine the worst-case run-time in terms of n (define (create-number-lists n) (cond [(= n 0) empty] [else (cons (build-list n identity) (create-number-lists (sub1 n)))]))

19 Determine the worst-case run-time in terms of n (define (create-a-list n) (cond [(even? n) empty] [else (build-list 3 (lambda (y) (+ n y)))]))


Download ppt "CS116 – Tutorial 3 Accumulative Recursion/Runtime."

Similar presentations


Ads by Google