Last active
July 24, 2017 21:57
-
-
Save denisdubovitskiy/42e9050f082136ffbd9e32af38178f64 to your computer and use it in GitHub Desktop.
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 print_list(lst): | |
| """for number in lst: print(number) | |
| Linear algorythm to recursion | |
| """ | |
| if not lst: | |
| return | |
| print(lst[0]) | |
| return print_list(lst[1:]) | |
| def sum_list_memo(lst, memo=0): | |
| if len(lst) == 0: | |
| return memo | |
| memo += lst[0] | |
| return sum_list_memo(lst[1:], memo) | |
| def sum_list_no_memo(lst): | |
| if len(lst) == 0: | |
| return 0 | |
| return lst[0] + sum_list_no_memo(lst[1:]) | |
| if __name__ == '__main__': | |
| print_list([0, 2, 3, 5, 6]) | |
| print sum_list_memo([0, 2, 3, 5, 6]) | |
| print sum_list_no_memo([0, 2, 3, 5, 6]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment