Hello, I need a program in Python that uses a generic sort function to do the fo
ID: 3822571 • Letter: H
Question
Hello, I need a program in Python that uses a generic sort function to do the following:
(i) sort numbers ascending by numerical value,
(ii) sort people alphabetically (lexicographically) by name, and to
(iii) sort people descending by age, where people of the same age should be sorted alphabetically (lexicographically).
The point here is to use the same function to do all 3 different sort operations. Try to reuse as much of your code and focus on clarity and brevity.
Data to use to sort:
The sequence of floating point numbers: (645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26)
The following sequence of people with name and age of each person. The name is a string and the age an integer: (Hal, 20; Susann, 31; Dwight 19; Kassandra, 21; Lawrence, 25; Cindy, 22; Cory, 27; Mac, 19; Romana, 27; Doretha, 32; Danna, 20; Zara, 23; Rosalyn, 26; Risa, 24; Benny, 28; Juan, 33; Natalie, 25)
Please use both of the data above in the answer for the correct answer.
This has to be in Python --Thanks
Explanation / Answer
Here is the solution:
d={'Rosalyn': 26, 'Romana': 27, 'Danny': 20, 'Hal': 20, 'Doretha': 32, 'Lawrence': 25, 'Juan': 33, 'Kassandra': 21, 'Benny': 28, 'Susann': 31, 'Cory': 27, 'Mac': 19, 'Natalie': 25, 'Cindy': 22, 'Naga': 28, 'Zara': 23, 'Risa': 24, 'Dwight': 19}
numbers= [645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26]
def sorting(arg):
if isinstance(arg,list):
print "Sort floating values: ", sorted(arg)
elif isinstance(arg,dict):
print "sort people alphabetically: " , sorted(arg.items(), key=lambda x:x[1])
count=0
k=sorted(arg.items(), key=lambda x:x[1],reverse=True)
while(count<(len(k)-1)):
if(k[count][0]>k[count+1][0] and k[count][1]==k[count+1][1]):
k[count],k[count+1]=k[count+1],k[count]
count=count+1
print "sort people descending by age, where people of the same age should be sorted alphabetically: ",k
output :
when you call sorting(numbers):
Sort floating values:
[-876.22, -34.94, -34.23, 1.11, 5.4, 23.37, 37.4, 62.26, 76.3, 467.73, 635.46, 645.32]
when you call sorting(d):
sort people alphabetically:
[('Mac', 19), ('Dwight', 19), ('Danny', 20), ('Hal', 20), ('Kassandra', 21), ('Cindy', 22), ('Zara', 23), ('Risa', 24), ('Lawrence', 25), ('Natalie', 25), ('Rosalyn', 26), ('Romana', 27), ('Cory', 27), ('Benny', 28), ('Naga', 28), ('Susann', 31), ('Doretha', 32), ('Juan', 33)]
sort people descending by age, where people of the same age should be sorted alphabetically:
[('Juan', 33), ('Doretha', 32), ('Susann', 31), ('Benny', 28), ('Naga', 28), ('Cory', 27), ('Romana', 27), ('Rosalyn', 26), ('Lawrence', 25), ('Natalie', 25), ('Risa', 24), ('Zara', 23), ('Cindy', 22), ('Kassandra', 21), ('Danny', 20), ('Hal', 20), ('Dwight', 19), ('Mac', 19)]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.