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 through each list.
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 through each list.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
Thank you for your reply. I think the autograder was grading differently for each type of lists. if len(list1) == len(list2), it wants the lists to be returned as merged without sorting. But if len(list1) > or < len(list2), then sort and return the sorted list. I have written the below, yet its not running right:
### START FUNCTION
def linear_merge(list1, list2):
# your code here
x = list1 + list2
if list1 == list2:
x1 = list1.extend(list2)
return x1
else:
x.sort()
return x
### END FUNCTION
### START FUNCTION
def linear_merge(list1, list2):
# your code here
x = list1 + list2
x.sort()
return x
### END FUNCTION
I have the solution above but which works but not in all cases. The autograder passed all others except this linear_merge([3, 2, 1], [5, 6, 1]). See the below:
FAILED linear_merge_is_linear
Inputs: [[3, 2, 1], [5, 6, 1]]
assert [1, 1, 2, 3, 5, 6] == [3, 2, 1, 5, 6, 1]
At index 0 diff: 1 != 3
Full diff:
expected output: [3, 2, 1, 5, 6, 1]
your output: [1, 1, 2, 3, 5, 6]
KIndly see