The question is :
Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list.
(a) Do this without using the sort method
I tried the following :
list1=input("Enter the elements of list :").split()
list2=input("Enter the elements of list :").split()
def merge(list1,list2):
merged_list,sorted_list=(list1+list2),[]
while len(merged_list)>0 :
sorted_list.append(min(merged_list))
merged_list.remove(min(merged_list))
return sorted_list
print(merge(list1,list2))
This works well for alphabetic strings, but doesn't work for numbers. It treats numbers as strings too and hence sorting them in dictionary order instead of sorting based on their numeric value. What changes should I incorporate to make the above code universal? It has to work well for both strings and numbers.