### 2017-03-10 - HW Fib

posted Mar 10, 2017, 6:58 AM by Samuel Konstantinovich   [ updated Mar 10, 2017, 7:19 AM ]
 Goal: Recursion day 2DO NOW: Open your exp(b,e) function and do the rest of the work on paper.Consider the following scheme functions:```(define (foo x) (if (< x 1) 1 (* x (foo (- x 1))))) ``````(define (bar x y) (cond (( > x y) 0)#base case (( = x y) x)#base case (else (+ x y (bar (+ x 1) (- y 1)))))) ````1. ON PAPER - Evaluate:``(foo 6)        (bar 1 3)        (bar 1 10)``2. ON PAPER - Write the function bunnyEars(n)``BEFORE WRITING IT: ``a) What is the base case?``b) How can you describe this recursively? (not with code)`We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).bunnyEars(0) → 0bunnyEars(1) → 2bunnyEars(2) → 4NOW consider the python function:```def what(s): total = 0 while len(s)>1: if s[:2]=="hi": total+=1 s = s[1:] return total``````3. ON PAPER - Evaluate:what("Hihihi") what("hi-hihihi")```4. Write a recursive version of the what() function. **This should use no loops, and no other methods that count.What is the base case? (easiest thing to count)What about all other strings, how can you reduce them to be closer to the base case?Homework - Bring in a printed physical copy, with your header on top. I WILL NOT count hand written work.Consider the Fibonacci sequence:0 1 1 2 3 5 8 13 21 34 55 . . .Or:n    nth term of the sequence0    01    12    13    24    35    5...The first two terms are 0 and 1. Each subsequent term is the sum of the previous two.0 10 + 1 -> 11 + 1 -> 21 + 2 -> 32 + 3 -> 5...QUESTIONS (Write complete answers.)1. Describe how this function has a recursive relationship2. Describe the relationship of fib(n) in terms of fib() 3. What must the base case(s) be?4. Draw a tree function calls when you want to evaluate: fib(5) ?5. How many times you need to call a fib() function for fib(5)?6. What happens to this diagram if you want to evaluate fib(6) ? Do NOT write the function.