$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #45

Which fix removes the bug?

def fib(n, memo={}):
    if n <= 1:
        return n
    if n in memo:
        return memo[n]
    return fib(n - 1) + fib(n - 2)
  1. Change n <= 1 to n < 1 in the base case
  2. Change the default to memo=None and init
  3. Store result: memo[n] = fib(n-1)+fib(n-2); return memo[n]
  4. Remove the if n in memo cache check line
Results are read from memo but never written, so it never caches.
Play Problem Solving ▸