Merge two sorted lists
# Input: lst1 {List}
# Input: lst2 {List}
# Output: {List}
#
# Example: merge([1, 4, 7], [2, 3, 6, 9]) => [1, 2, 3, 4, 6, 7, 9]
#
def merge(items1, items2):
# merge 2 list
# divide and conquer
# combine - step2
("\n"
" while left_index is not the length of left_array and right_index is not the length of right_array\n"
" if left_array[left_index] < right_array[right_index]\n"
" add left_array[left_index] to merged array\n"
" left_index + 1\n"
" else\n"
" add right_array[right_index] to merged_array\n"
" right_index + 1 ")
# if we've passed the end of either list, then extend the sorted list
# with what remains in the other
left_pointer = 0
right_pointer = 0
sorted_list = []
while len(sorted_list) < len(items1) + len(items2):
left_item = items1[left_pointer]
right_item = items2[right_pointer]
if left_item < right_item:
sorted_list.append(left_item)
left_pointer += 1
else:
sorted_list.append(right_item)
right_pointer += 1
if right_pointer >= len(items2):
sorted_list.extend(items1[left_pointer:])
break
if left_pointer >= len(items1):
sorted_list.extend(items2[right_pointer:])
break
return sorted_list
test cases that i am working on
print('merge tests')
test_count = [0, 0]
def test():
results = merge([1, 4, 7], [2, 3, 6, 9])
return (len(results) == 7 and
results[0] == 1 and
results[1] == 2 and
results[2] == 3 and
results[3] == 4 and
results[4] == 6 and
results[5] == 7 and
results[6] == 9)
expect(test_count, 'should return [1, 2, 3, 4, 6, 7, 9] when merging [1, 4, 7]', test)
def test():
results = merge([], [2, 3, 6, 9])
return (len(results) == 4 and
results[0] == 2 and
results[1] == 3 and
results[2] == 6 and
results[3] == 9)
expect(test_count, 'should handle inputs when left argument is empty array', test)
def test():
results = merge([1, 4, 7], [])
return (len(results) == 3 and
results[0] == 1 and
results[1] == 4 and
results[2] == 7)
expect(test_count, 'should handle inputs when right argument is empty array', test)
def test():
results = merge([], [])
return len(results) == 0
expect(test_count, 'should handle two empty arrays as inputs', test)
print('PASSED: ' + str(test_count[0]) + ' / ' + str(test_count[1]) + '\n\n')
class TestObject(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, other):
return self.x < other.x
def __str__(self):
return "LTO (%d, %d)" % (self.x, self.y)
results == [1,2,3,4,6,7,9]
\$\endgroup\$