Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Rank the marks {"Asha": 92, "Ravi": 78, "Meera": 85, "Kabir": 64, "Zoya": 78} from the highest. Two students have . In what order do they come?
A key can be a pair: the second part breaks a tie.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
The sort key is (-mark, name): the highest mark first, and for equal marks the names in alphabetical order.
Ravi is before Zoya because R comes before Z. Without the second part of the key, a tie would be left in the order in which Python happened to find them.
Log in to join the discussion.
Loading…