0

Hi how is it that these two codes seem to run on identical time ?

The first code is a standard fibonacci memoisation

def fib(n, memo = {}):
    if n in memo:
        return memo[n]
    if n <= 2:
        return 1
    memo[n] = fib(n-1, memo) + fib(n-2, memo)
    return memo[n]

The second one is the exact same code, but this time the memos are not passed

def fib(n, memo = {}):
    if n in memo:
        return memo[n]
    if n <= 2:
        return 1
    memo[n] = fib(n-1) + fib(n-2)
    return memo[n]

I presumed that the sub calls would then initialise default empty memo, meaning that essentially there is no memoisation but it seems that these two codes actually run identically. What am I missing here ? thanks

0 Answers0