Last active
November 24, 2022 19:24
-
-
Save bDrwx/25970bf17d21717eba6e019f7a555790 to your computer and use it in GitHub Desktop.
Example recursion functions
This file contains 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 sum(arr): | |
if not arr: | |
return 0 | |
return arr.pop(0) + sum(arr) | |
def count(arr): | |
if not arr: | |
return 0 | |
return 1+count(arr[1:]) | |
def max(arr, high): | |
''' | |
TODO https://stackoverflow.com/questions/12711397/python-recursive-function-to-find-the-largest-number-in-the-list | |
''' | |
if not arr: | |
return high | |
tmp = arr.pop(0) | |
if tmp > high: | |
return max(arr, tmp) | |
else: | |
return max(arr, high) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment