Start free

Intermediate · Sorting · 147 of 264

The podium

Print the top three scores with their names, highest first. On a tie, the name that comes first alphabetically wins.

Input: n (at least 3), then n lines "name score". Output: three lines: <place>. <name> <score>

Examples

Input
5
ann 90
bob 95
cid 80
dan 95
eve 70
Output
1. bob 95
2. dan 95
3. ann 90
Input
3
a 1
b 2
c 3
Output
1. c 3
2. b 2
3. a 1

One more test checks other inputs when you press Check.

Stuck? The idea behind sorting

sorted() gives a new sorted list; list.sort() sorts the list itself. reverse=True sorts from largest to smallest.

key= says what to sort by: key=len sorts words by length, key=lambda p: p[1] sorts pairs by their second item.

people = [("Ann", 14), ("Bob", 12)]
for name, age in sorted(people, key=lambda p: p[1]):
    print(name, age)

Watch for: list.sort() gives back None: names = names.sort() loses the list. Use sorted() for a new one.

All the sorting exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More sorting exercises