Q. Given the following list L that stores the names of Nobel prize winners. Each element of the list is a tuple containing details of the one noble prize winner.
L = [(“Wilhelm Conrad R ontgen”, “physics”,1901),(“Ronald Ross”, “medicine”,1902),(“Marie curie ”, “physics ”,1903),(“Ival Pavlov ”, “medicine”,1904),(“Henryk Sienkiewicz ”, “literature”,1905),(“Theodore Roosevelt ”, “peace”,1904)]
Write a program to short the list in the order of last names of the winners. use insertion sort algorithm.
Answer =
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)] st = '' lst = [] x = 0 for a in L : st = '' for b in a[0]: if b == ' ' : st = '' else : st = st + b lst.insert(x,st) x = x + 1 length = len(L) for i in range (1 , length) : temp = lst [ i ] extemp = L[ i ] j = i - 1 while j >=0 and temp < lst[j]: L[j+1] = L[j] lst[j+1] = lst[j] j = j - 1 L[j+1] = extemp lst[j+1] = temp for x in L: print(x)
Output :-
('Marie Curie', 'Physics', 1903)
('Wilhelm Conrad R Ontgen', 'Physics', 1901)
('Ivan Pavlov', 'Medicine', 1904)
('Theodore Roosevelt', 'Peace', 1906)
('Ronald Ross', 'Medicine', 1902)
('Henryk Sienkiewicz', 'Literature', 1905)
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )