Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write and use a generic sort function in Python. Use duck-typing in Python if po

ID: 3815070 • Letter: W

Question

Write and use a generic sort function in Python. Use duck-typing in Python if possible.

Here are the guidelines:

Use your generic sort function to

(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

Thanks a lot

Explanation / Answer

student_tuples = [('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)]
print('SORTING',sorted(student_tuples, key=lambda student: student[2]))
# sort by age

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote