sa 11 cs chapter 14

2. Given following list L that stores the names of Nobel prize winners . Each element of the list is a tuple containing details of one Nobel prize winner. L = [ (“Wilhelm Conrad R’ontgen”, “Physics”, 1901), (“Ronald Ross”, “Medicine”, 1902), (“Marie Curie”, “Physics”, 1903) (“Ivan Pavlov”, “Medicine”, 1904), (“Henryk Sienkiewicz”, “Literature”, 1905), (“Theodore Roosevelt”, “Peace”, 1906)] Write a program to sort the list in the order of Last names of the winners. Use insertion sort algorithm.

def insertion_sort(arr): for i in range(1,len(arr)): key = arr[i] k = arr[i][0].split()[-1] # split() function is splitting the names using spaces and then [-1] is used to capture the last name j = i-1 while j >= 0 and k < arr[j][0].split()[-1]: arr[j+1] = arr[j] j -= 1 arr[j+1] = key return arr L = […]

2. Given following list L that stores the names of Nobel prize winners . Each element of the list is a tuple containing details of one Nobel prize winner. L = [ (“Wilhelm Conrad R’ontgen”, “Physics”, 1901), (“Ronald Ross”, “Medicine”, 1902), (“Marie Curie”, “Physics”, 1903) (“Ivan Pavlov”, “Medicine”, 1904), (“Henryk Sienkiewicz”, “Literature”, 1905), (“Theodore Roosevelt”, “Peace”, 1906)] Write a program to sort the list in the order of Last names of the winners. Use insertion sort algorithm. Read More »

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 »