Created
November 24, 2018 17:37
-
-
Save Ojha-Shashikant/7f603cae3942fecf8117b365f7ec5e06 to your computer and use it in GitHub Desktop.
Taking 2 sorted lists as argument, combine both the lists and print the sorted final list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' Taking 2 sorted lists as argument, combine both the lists and print the sorted final list ''' | |
def input_number(): | |
return input("Please enter numbers for your list: ") | |
def input_list(): | |
num = input_number() | |
list_input = list() | |
while num!= '': | |
list_input.append(int(num)) | |
num = input_number() | |
return list_input | |
def sorting(any_list): | |
for i in range(0, len(any_list)): | |
for j in range(i+1, len(any_list)): | |
if any_list[i] > any_list[j]: | |
any_list[i], any_list[j] = any_list[j], any_list[i] | |
return any_list | |
def list_merging(list1, list2): | |
merged_list = list1 + list2 | |
return merged_list | |
def test_function(merged_sorted_lists, sorted_merged_sorted_list): | |
x = sorted(merged_sorted_lists) | |
if x == sorted_merged_sorted_list: | |
print("Test Successful !!!") | |
return True | |
else: | |
print("Test Failed") | |
return False | |
# main starts here | |
if __name__ == '__main__': | |
print("List 1 entry starts:") | |
list1 = input_list() | |
print("List 1: ", list1) | |
sorted_list1 = sorting(list1) | |
print("sorted list1:", sorted_list1) | |
print() | |
print("List 2 entry starts:") | |
list2 = input_list() | |
print("List 2: ", list2) | |
sorted_list2 = sorting(list2) | |
print("sorted list2:", sorted_list2) | |
print() | |
merged_lists = list_merging(sorted_list1, sorted_list2) | |
print("merged sorted lists:", merged_lists) | |
print() | |
#to_be_tested_merged_lists = list() | |
#to_be_tested_merged_lists = merged_lists | |
#print("to_be_tested_merged_lists", to_be_tested_merged_lists) | |
sorted_merged_list = sorting(merged_lists) | |
print("sorted merged sorted list:", sorted_merged_list) | |
print() | |
#print("again printing", to_be_tested_merged_lists) | |
result = test_function(merged_lists, sorted_merged_list) | |
if result == True: | |
left = [4, 17, 21, 47, 69, 75, 91, 97] | |
right = [3, 10, 11, 14, 18, 21, 44, 55, 76, 97] | |
merged_lists = list_merging(left, right) | |
sorted_merged_list = sorting(merged_lists) | |
print("Test merged sorted list:", sorted_merged_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1 issue in s15q02_solution.py, I was not able to test the function, it was always showing successful because once the list sorted even the copy of lists were also changed original list was lost in all cases. My sorting is working as sort(), but copy of a list is also pointing to the same memory and changed. Can be seen by un-commenting the commented statements in the function.