0
# -*- coding: utf-8 -*-
def memoize(limit, *, message = 'Limit exceded'):
count = 0
def inner(func):
cache = {}
def wrapped(number):
nonlocal count
if count < limit:
if number not in cache:
cache[number] = func(number)
count += 1
return cache[number]
print(message)
return wrapped
return inner
@memoize(5)
def fat(x):
if x < 2:
return 1
return x * fat(x - 1)
In theory the algorithm should receive a number that would define a storage limit of the results in a cache, instead of raising an exception I simply show the message that was passed or the default ("Limit exceeds") if the limit number in the cache is reached. The problem is that it only runs the program once and shows the message, but where is the error ???
From what I understand in your code you are taking advantage of the fact that there is a recursion in the code and incrementing and decrementing the recursing variable with each recursion passage ???
– ThiagoO
The code is not "taking advantage" of anything - it know that is in a recursion because that variable is incremented/decremented
– jsbueno