mardi 30 décembre 2014

Why is this not assigning the correct value?


Vote count:

0




I wanted to store the biggest fib number calculated so far, but the variable biggest is not being assigned to correctly.



cache = {}
biggest = 1
def fib(n):
if n == 0:
return 0
if n == 1:
return 1

if not n in cache:
cache[n] = fib(n-1) + fib(n-2)
print "calculated %ith fib giving %i" %(n, cache[n])
biggest = cache[n]

return cache[n]


After doing fib(10), the cache looks good, but biggest is still 1. Sorry for the beginner question, but was hoping someone could enlighten me. Thanks!



asked 56 secs ago







Why is this not assigning the correct value?

Aucun commentaire:

Enregistrer un commentaire