livepython.net

251. Python Program to Make a Game Leaderboard

MediumPhase 2 · Practice

This one takes a few steps. You know everything it uses: a dictionary, a list with sort() and a loop that finds the biggest value.

Overview

A leaderboard shows the best players of a game, from the most points down. Games and sports apps show one after every round.

Problem Statement

Hiro runs a game night. After each round, Hiro writes down the points each player won, one line per player, like Mia 10. A player can appear many times, so add up all their points. Read the lines until a line says end.

Then print the top 3 players, from the most points to the fewest, as <place>. <name> <points>. The places are 1, 2 and 3. If there are fewer than 3 players, print them all. If two players have the same points, the one whose name comes first from A to Z goes first, and the places still count 1, 2, 3. If there are no players at all, print only No players.

Examples

Example 1. Input:

Mia 10
Leo 7
Mia 5
Ivan 12
Leo 9
Zoe 3
end

gives:

1. Leo 16
2. Mia 15
3. Ivan 12

Why: Leo has 7 + 9 = 16, Mia has 10 + 5 = 15, Ivan has 12 and Zoe has 3. Zoe is fourth, so Zoe is not shown.

Example 2. Input:

Tariq 8
Ana 5
Tariq 0
Ana 3
end

gives:

1. Ana 8
2. Tariq 8

Why: both players have 8 points, so A to Z decides: Ana comes before Tariq. There are only 2 players, so only 2 lines are printed.

Input

One line per player per round: a name, a space, then the points, a whole number of 0 or more. A name is one word that starts with a capital letter, followed by small letters. The last line is always end. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

Up to 3 lines, or only No players. The tests try 7 different inputs, also one player, ties for third place and a tie where the player who came first in the input is second from A to Z.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 13 · Dictionaries
solution.py
# write your code here

EXPECTED OUTPUT

11. Leo 16
22. Mia 15
33. Ivan 12

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.