patternpythonMinor
Merge two sorted lists of numbers
Viewed 0 times
mergenumberstwolistssorted
Problem
Inputs are two sorted lists (of the same length) of numbers. I would like to return a new merged, sorted new list. I have written the following code, but most lines are used to deal with the end case. I am wondering if there is any better way to write it.
def merge(array1,array2):
result = [0]*len(array1)*2
i = 0 # left index
j = 0 # right index
for k in range(0,len(result)):
# when none of the pointer is at the end of the list
if i != len(array1)-1 and j != len(array2)-1:
if array1[i] array2[j]:
result[k] = array2[j]
j = j + 1
# the following codes are used to deal with the end cases.
# possible to write it more compactly?
elif i == len(array1)-1:
if j > len(array2)-1:
result[-1] = array1[-1]
return result
elif array1[i] len(array1)-1:
result[-1] = array2[-1]
elif array2[j] < array1[i]:
result[k] = array2[j]
result[(k+1):] = array1[i:]
return result
else:
result[k] = array1[i]
i = i + 1
return resultSolution
I'll answer your question by providing an alternative solution. IDK why you're over complicating this when everything that has to be done is to combine the two lists, then simply sort them:
The advantages of the above solution are:
There's also a one-liner which is even simpler than the above solution:
You could also use the built-in heapq.merge for this.
Of course, if the lists are very large (like very large), merging two lists will totally make sense.
According to this SO answer unless
Those are just my 2-cents regarding the subject. I'll let somebody else comment on your code.
Extra: Even if practicing algorithms is a good idea, an even nicer aspect is knowing when / where to apply them.
def merge(array1, array2):
array1.extend(array2)
return sorted(array1)print(merge([1, 3, 4, 7], [0, 2, 5, 6, 8, 9]))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]The advantages of the above solution are:
- there's less code so it's easier to maintain / read
- it's using only two built-in functions (so assuming the lists are of a reasonable size, it should be quicker than implementing the sorting/merging in a loop)
There's also a one-liner which is even simpler than the above solution:
def merge(array1, array2):
return sorted(array1 + array2)print(merge([1, 3, 4, 7], [0, 2, 5, 6, 8, 9]))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]You could also use the built-in heapq.merge for this.
Of course, if the lists are very large (like very large), merging two lists will totally make sense.
According to this SO answer unless
len(array1 + array2) ~ 1000000 use:L = array1 + array2
L.sort()Those are just my 2-cents regarding the subject. I'll let somebody else comment on your code.
Extra: Even if practicing algorithms is a good idea, an even nicer aspect is knowing when / where to apply them.
Code Snippets
def merge(array1, array2):
array1.extend(array2)
return sorted(array1)print(merge([1, 3, 4, 7], [0, 2, 5, 6, 8, 9]))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]def merge(array1, array2):
return sorted(array1 + array2)print(merge([1, 3, 4, 7], [0, 2, 5, 6, 8, 9]))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]L = array1 + array2
L.sort()Context
StackExchange Code Review Q#148961, answer score: 4
Revisions (0)
No revisions yet.