[Tutor] lists+sort
Pooja Bhalode
poojabhalode11 at gmail.com
Mon Jan 4 12:34:57 EST 2016
Hi, I wanted to check if this program can be used to merge the lists
together and sort them. This seems to work, but i wanted to check if there
are drawbacks in writing it in this manner.
My solution:
def linear_merge(list1, list2):
for num in list2:
list1.append(num)
list1.sort()
# +++your code here+++
return list1
Whereas, their code is a bit different, I have posted it here.
def linear_merge(list1, list2):
result = []
# Look at the two lists so long as both are non-empty.
# Take whichever element [0] is smaller.
while len(list1) and len(list2):
if list1[0] < list2[0]:
result.append(list1.pop(0))
else:
result.append(list2.pop(0))
# Now tack on what's left
result.extend(list1)
result.extend(list2)
return result
Can you please tell me if there is a problem in the first code?
Thank you.
More information about the Tutor
mailing list