Skip to content

Instantly share code, notes, and snippets.

@renzon
Created November 4, 2016 10:53
Show Gist options
  • Save renzon/75cab4c70dfd7d2eaff8bb1db75f8ed5 to your computer and use it in GitHub Desktop.
Save renzon/75cab4c70dfd7d2eaff8bb1db75f8ed5 to your computer and use it in GitHub Desktop.
find_max recursive
def find_max(iterable):
lst = list(iterable)
n = len(lst)
if n == 0:
raise Exception('Not possible finding max of empty')
def find_iter(current_max, i):
if i == n:
return current_max
current = lst[i]
return find_iter(
current_max if current_max >= current else current,
i+1
)
return find_iter(lst[0], 1)
print(find_max(range(3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment