Update 04_recursive_max.py (#52)

Avoid error while len(lst) is 0 and 1
This commit is contained in:
WangX
2018-02-04 03:57:39 +08:00
committed by Aditya Bhargava
parent b515434b3f
commit 3474a1069f

View File

@@ -1,5 +1,8 @@
def max(list): def max_(lst):
if len(list) == 2: if len(lst) == 0:
return list[0] if list[0] > list[1] else list[1] return None
sub_max = max(list[1:]) if len(lst) == 1:
return list[0] if list[0] > sub_max else sub_max return lst[0]
else:
sub_max = max_(lst[1:])
return lst[0] if lst[0] > sub_max else sub_max