From 308148653af51d87b8e8622383c63a4bda775d88 Mon Sep 17 00:00:00 2001 From: Udit Gupta Date: Fri, 18 Nov 2022 13:47:57 -0800 Subject: [PATCH] Enhances 'base case' logic (#227) Original function does not work when input array is empty or contains a single value. In the revised code, base case has been augmented to support those. Works successfully for all these test cases: find_max([1, 12, 8, 5]), find_max([]), find_max([1]) --- 03_recursion/python/06_find_max.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/03_recursion/python/06_find_max.py b/03_recursion/python/06_find_max.py index 9080084..bb5afc5 100644 --- a/03_recursion/python/06_find_max.py +++ b/03_recursion/python/06_find_max.py @@ -1,5 +1,9 @@ def find_max(arr): - if len(arr) == 2: + if len(arr) == 0: + return 0 + elif len(arr) == 1: + return arr[0] + elif len(arr) == 2: return arr[0] if arr[0] > arr[1] else arr[1] sub_max = find_max(arr[1:]) - return arr[0] if arr[0] > sub_max else sub_max \ No newline at end of file + return arr[0] if arr[0] > sub_max else sub_max