Skip to content

Instantly share code, notes, and snippets.

@bDrwx
Last active November 24, 2022 19:24
Show Gist options
  • Save bDrwx/25970bf17d21717eba6e019f7a555790 to your computer and use it in GitHub Desktop.
Save bDrwx/25970bf17d21717eba6e019f7a555790 to your computer and use it in GitHub Desktop.
Example recursion functions
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