From 3474a1069ff718c0087b379b2d33eb0a1c839b49 Mon Sep 17 00:00:00 2001 From: WangX Date: Sun, 4 Feb 2018 03:57:39 +0800 Subject: [PATCH] Update 04_recursive_max.py (#52) Avoid error while len(lst) is 0 and 1 --- 04_quicksort/python/04_recursive_max.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/04_quicksort/python/04_recursive_max.py b/04_quicksort/python/04_recursive_max.py index c32fb54..908c31a 100644 --- a/04_quicksort/python/04_recursive_max.py +++ b/04_quicksort/python/04_recursive_max.py @@ -1,5 +1,8 @@ -def max(list): - if len(list) == 2: - return list[0] if list[0] > list[1] else list[1] - sub_max = max(list[1:]) - return list[0] if list[0] > sub_max else sub_max +def max_(lst): + if len(lst) == 0: + return None + if len(lst) == 1: + return lst[0] + else: + sub_max = max_(lst[1:]) + return lst[0] if lst[0] > sub_max else sub_max