The question comes from this nice blog on programming. I will solve it without giving much details, and test it quickly.
def merge_two(list1, list2):
if len(list1) == 0:
return list2[:]
elif len(list2) == 0:
return list1[:]
else:
if list1[0] < list2[0]:
return [list1[0]] + merge_two(list1[1:], list2)
else:
return [list2[0]] + merge_two(list1, list2[1:])
def merge(*lists):
head = []
for list_i in lists:
head = merge_two(head, list_i)
return head
import random
def random_sorted_list(size):
return sorted([random.randint(0, 100) for _ in range(size)])
def issorted(alist):
return alist == sorted(alist)
for size in [10, 20, 30]:
for k in range(2, 20):
lists = [random_sorted_list(size) for _ in range(k)]
merged_list = merge(*lists)
assert issorted(merged_list)
One can prove that the algorithm we proposed is:
Et voilĂ .