Why the output would be as follows:
49 5341 5387 5387 5323 87...
As far as my understanding goes,
Initial call as:
items[0] = 6val2 = find_max([20, 8, 19, 56, 23, 87, 41, 49, 53])
Recursive call:
val1 = 20val2 = find_max([8, 19, 56, 23, 87, 41, 49, 53])
The code as below:
items = [6, 20, 8, 19, 56, 23, 87, 41, 49, 53]def find_max(items): # breaking condition: last item in list? return it if len(items) == 1: return items[0] # otherwise get the first item and call function # again to operate on the rest of the list val1 = items[0] val2 = find_max(items[1:]) # perform the comparison when we're down to just two if val1 > val2: return val1 else: return val2# test the functionprint(find_max(items))
49 5341 5387 5387 5323 8756 8719 878 8720 876 8787