PROBLEM E.: Given two lists sorted in increasing order, create and return a merged
list of all the elements in sorted order. You may modify the passed in lists.
Ideally, the solution should work in "linear" time, making a single
pass of both lists.
GIVEN SOLUTION:
def linear_merge(list1, list2):
result = []
while len(list1) and len(list2):
if list1[0] < list2[0]:
result.append(list1.pop(0))
else:
result.append(list2.pop(0))
result.extend(list1)
result.extend(list2)
return result
MY SOLUTION:
def linear_merge(list1, list2):
list1.extend(list2)
return sorted(list1)
I try to work out the problem first before confirming with the provided solution. This is what I came up with after reading this exercise problem. It sounded too simple, so I looked at their solution and became confused. BUT, no matter how many times I test it, I always get the correct answer, same as theirs. My question is, maybe I'm testing too simple examples (although I tried to mix it up), could this be right? And what would ultimately trip me into bad answer or error? Thanks.