In fact, if there is a mistake in that ‘else’ block, our function will not fail if not tested thoroughly.
return lst1
else:
return lst997 # this list is not defined, but the function didn't complain when run our test example
#Uncomment the line below when your function is done
print(larger_sum([1, 9, 5], [2, 3, 7]))
Output:
[1, 9, 5]
My solution is recursive: I had stuck trying to mimic sum() for an unknown me reason:
def larger_sum(lst1, lst2):
def additionx(lst1): # defining a helper function of my custom sum()
while len(lst1) > 0: #set the rule of allowed run
first_element = next(iter(lst1)) #a trick to find 1st element of the list, for..in works too
if len(lst1) == 1: # set the exit of recursion
return lst1[0]
return first_element + additionx(lst1[1:]) #recurse
if additionx(lst1) >= additionx(lst2): #compare by call of a custom sum()
return lst1
return lst2 #no need of else, due to only one other way out