Last active
September 9, 2016 03:08
-
-
Save lvsmart/459efb993ea7d56a9337cc524561f788 to your computer and use it in GitHub Desktop.
test_decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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