Challenge: Leaderboard
Completed
Read n players with a name and a score. Print them from highest score to lowest; players with the same score are listed alphabetically. Use sorted with a key.
Input
Line 1: n. Then n lines name score.
Output
One line per player: name score.
Example
Input
4 Ravi 78 Asha 91 Meera 91 Zoya 85
Output
Asha 91 Meera 91 Zoya 85 Ravi 78
Editor
Python
Hints
Store (name, score) tuples.
key=lambda p: (-p[1], p[0]) sorts by score high-to-low, then name A-to-Z.
Solution explanation
A tuple key sorts by the first part, then uses the second part to break ties.
Show solution
n = int(input())
players = []
for _ in range(n):
name, score = input().split()
players.append((name, int(score)))
for name, score in sorted(players, key=lambda p: (-p[1], p[0])):
print(name, score) Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module