Skip to content

Instantly share code, notes, and snippets.

@lvsmart
Last active September 9, 2016 03:08
Show Gist options
  • Select an option

  • Save lvsmart/459efb993ea7d56a9337cc524561f788 to your computer and use it in GitHub Desktop.

Select an option

Save lvsmart/459efb993ea7d56a9337cc524561f788 to your computer and use it in GitHub Desktop.
test_decorator
def fibonacci(n, cache=None):
if cache is None:
cache = {}
if n in cache:
return cache[n]
if n <=1:
return 1
cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache)
return cache[n]
print(fibonacci(30))
# 1346269
print(fibonacci(50))
# 20365011074
def memo(func):
cache = {}
def wrap(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrap
@memo
def climb(n, steps):
count = 0
if n == 0:
count = 1
elif n > 0:
for step in steps:
count += climb(n - step, steps)
return count
print(climb(30,(1,2,3)))
# 53798080
def memo(func):
cache = {}
def wrap(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrap
@memo
def fibonacci(n):
if n <= 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
# fibonacci = memo(fibonacci)
# print(fibonacci(50))
print(fibonacci(30))
# 1346269
print(fibonacci(50))
# 20365011074
def fibonacci(n):
if n <= 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(30))
# 1346269
print(fibonacci(50))
# timeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment