Menu
Coddy logo textTech

Recap - Leaderboard

Part of the Logic & Flow section of Coddy's Swift journey — lesson 32 of 56.

challenge icon

Challenge

Medium

You're producing a leaderboard. Read a single line of input: a comma-separated list of name:score entries. Some entries have a non-integer score and should be skipped (use compactMap).

Print the top three players by score descending, one per line, in the format:

<rank>. <name> - <score>

Ranks start at 1. Break ties by name alphabetically. If fewer than three valid entries exist, print all of them.

After the leaderboard, print Avg: <n> with the integer average of every valid score (truncate toward zero, no decimals). When no valid entries exist, print Avg: 0.

For input alice:90,bob:bad,cara:85,dan:90,eve:70,fin:99, the output is:

1. fin - 99
2. alice - 90
3. dan - 90
Avg: 86

Try it yourself

let entries = readLine()!.components(separatedBy: ",")

// TODO: compactMap to (name, score); sort by score desc tiebreak by name;
// print top 3; then average of all valid scores

All lessons in Logic & Flow