cs116 – tutorial 3 accumulative recursion/runtime

19
CS116 – Tutorial 3 Accumulative Recursion/Runtime

Upload: job-hoover

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS116 – Tutorial 3 Accumulative Recursion/Runtime

CS116 – Tutorial 3Accumulative Recursion/Runtime

Page 2: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Reminders:Assignment 3 is due Wednesday

at 10 am.

Page 3: CS116 – Tutorial 3 Accumulative Recursion/Runtime

ReviewAccumulative RecursionRun Time

Page 4: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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

Page 5: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Question 1Write 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.

Page 6: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Trace ComparisonWithout 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

Page 7: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Trace ComparisonWith 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

Page 8: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Question 2Develop 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))

Page 9: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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

Page 10: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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.

Page 11: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Question 4Write 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.

Page 12: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Runtime ReviewLook at the “worst case” scenario

(i.e. longest time)Only for code that gets executed

when you run itAssume function works (i.e. will

not produce an error when you run it)

Page 13: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Runtime ReviewO(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)

Page 14: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Runtime ReviewO(n2) – Quadratic

◦time proportional to square of input(map sqr (build-list n add1))Ex: nested abstract list function, O(n) done multiple times

O(2n) – Exponential◦As size of input increases, run time

doubles

Module 3, Slide 22: fib

Ex: Multiple function calls (>1 calls in one cond case)

Page 15: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Question 5

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

Page 16: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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)))]))

Page 17: CS116 – Tutorial 3 Accumulative Recursion/Runtime

Determine worst-case run-time in terms of n, assuming n elements in loi

(define (evens loi)(filter even? loi))

Page 18: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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)))]))

Page 19: CS116 – Tutorial 3 Accumulative Recursion/Runtime

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)))]))