I came across a weird phenomenon:
I wrote a code to calculate "Catalan Numbers", which works , but now I'm trying to improve run-time by using a Memoization dictionary (called it dicatalan):
dicatalan = {}
def catalan(n):
if n == 0:
return 1
else:
res = 0
if n not in dicatalan:
for i in range(n):
res += catalan(i) * catalan(n - i - 1)
dicatalan[n] = res
print ("dicatalan is", dicatalan)
return dicatalan[n]
Here's the catch - In eclipse - Pydev - for n=1
the code runs halfway and prints as expected: "dicatalan is 1:1" before stopping mysteriously, but in IDLE the same code prints "dicatalan is 0:1" .
Any case, when trying to print later dicatalan I received {}.
How could that be? what happens in the code? running debugger proved futile.
Any ideas for making the dict work?