list.insert
returns None, not the modified list.
This requires helper
to be changed to read
def helper(list1, list2, result):
if not list1:
return result + list2
elif not list2:
return result + list1
elif list1[0] < list2[0]:
return helper(list1[1:], list2, result + [list1[0]])
else:
return helper(list1, list2[1:], result + [list2[0]])
Note the changes to the two base cases. None
and the empty list []
are not the same thing. The pythonic way of testing if a list is empty is to treat the list as a Boolean value: empty lists are False
, all others are True
.
And as the others have noticed before me, you need to explicitly return the return value of helper
in linear_merge
.