Skip to content

Instantly share code, notes, and snippets.

@Sigmanificient
Created June 14, 2022 21:55
Show Gist options
  • Save Sigmanificient/8cc58ac190b06df17d749cd33760c59e to your computer and use it in GitHub Desktop.
Save Sigmanificient/8cc58ac190b06df17d749cd33760c59e to your computer and use it in GitHub Desktop.
A decorator that remove reccursion limit on simple recursive function
def unrecursify(func):
class Unrecursify(Exception):
pass
call_args = []
call_result = []
def out(*_args):
return call_result[-1]
def inner(*args):
call_args.append(args)
raise Unrecursify()
def wrapper(*args):
bak = func
globals()[func.__name__] = inner
call_args.append(args)
while True:
try:
bak(*call_args[-1])
except Unrecursify:
continue
else:
break
globals()[func.__name__] = out
for c, f_args in enumerate(call_args[::-1]):
call_result.append(bak(*f_args))
globals()[func.__name__] = unrecursify(bak)
return call_result[-1]
return wrapper
def scientific_notation(n):
n_str = str(n)
return f'{n_str[0]}.{n_str[1:32]}x10^{len(n_str) - 1}'
@unrecursify
def factorial(n):
return n * factorial(n-1) if n else 1
x = factorial(5000)
print(scientific_notation(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment