I have to write a fib_memoize(n)
function that should be recursive but it should memoize its answer so that it's running time is O(n)
.
This is what I have:
def fib_memoize(n):
memo = {}
if n in memo:
return memo[n]
else:
if n <=2:
f = 1
else:
f = fib_recursive(n-1) + fib_recursive(n-2)
memo[n] = f
return f
This is giving me an assertion error saying 1 != 0
. What is this pointing to? What am I missing?
This is my fib_recursive
function (passes the tests):
def fib_recursive(n):
if n <=1:
return n
else:
return fib_recursive(n-1) + fib_recursive(n-2)