sa 11 cs chapter 14

3. Write a program to read a list containing 3-digit integers only. Then write an insertion sort program that sorts the list on the basis of one’s digit of all elements. That is, if given list is :

[387, 410, 285, 106] then the sorted list (as per above condition) should be : [410, 285, 106, 387] “One’s digits are in sorted order (0, 5, 6, 7). For two matching one’s digits, the order remains as in the original list. # Code def insertion_sort(arr): for i in range(1,len(arr)): key = arr[i] j = […]

3. Write a program to read a list containing 3-digit integers only. Then write an insertion sort program that sorts the list on the basis of one’s digit of all elements. That is, if given list is : Read More »

4. Write a program to perform sorting on a given list of strings, on the basis of length of strings. That is, the smallest-length string should be the first string in the list and the largest-length string should be the last string in the sorted list.

# Code def insertion_sort(arr): for i in range(1,len(arr)): key = arr[i] j = i-1 while j >= 0 and len(key) < len(arr[j]): # comparison on the basis of length arr[j+1] = arr[j] j -= 1 arr[j+1] = key return arr l = input("Enter a list of strings : ").split() # split() splits the input on

4. Write a program to perform sorting on a given list of strings, on the basis of length of strings. That is, the smallest-length string should be the first string in the list and the largest-length string should be the last string in the sorted list. Read More »