sa 11 cs chapter 14

1. Election is a dictionary where key : value pairs are in the form of name:votes received. Write a program that sorts the contents of the Election dictionary and creates two lists that stores the sorted data. List A[i] will contain the name of the candidate and ListB[i] will contain the votes received by candidate ListA[i]. Print the name of candidates with votes received in descending order of the number of votes received.

def bubble_sort(arr): for i in range(len(arr)-1): for j in range(i+1, len(arr)): if arr[i][1] < arr[j][1]: arr[i], arr[j] = arr[j], arr[i] return arr Election = {'Abdul':500, 'Mohan':395, 'Amir':800, 'Atishi':1000, 'Gautam':900} e = bubble_sort(list(Election.items())) # Election.items() returns a list of tuples with each tuple being key:value pair from the dictionary A1 = [e[i][0] for i in range(len(e))] […]

1. Election is a dictionary where key : value pairs are in the form of name:votes received. Write a program that sorts the contents of the Election dictionary and creates two lists that stores the sorted data. List A[i] will contain the name of the candidate and ListB[i] will contain the votes received by candidate ListA[i]. Print the name of candidates with votes received in descending order of the number of votes received. Read More »

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 »