1.

Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list.Write a function called merge that takes two already sorted lists of possibly differentlengths, and merges them into a single sorted list.​

Answer»

st1 = [1, 5, 6, 9, 11]test_list2 = [3, 4, 7, 8, 10]  # printing ORIGINAL lists print ("The original LIST 1 is : " + str(test_list1))print ("The original list 2 is : " + str(test_list2))  # USING naive METHOD # to combine two sorted listssize_1 = len(test_list1)size_2 = len(test_list2)  res = []i, j = 0, 0  while i < size_1 and j < size_2:    if test_list1[i] < test_list2[j]:      res.append(test_list1[i])      i += 1      else:      res.append(test_list2[j])      j += 1  res = res + test_list1[i:] + test_list2[j:]



Discussion

No Comment Found