The Top of the Leaderboard

Working with ListsEasy+10 XP+10

The Top of the Leaderboard

A quiz app keeps every player's score in a list, in the order the games were played. The results screen shows the top k scores, highest first.

Task: write top_k(scores, k) that returns a new list holding the k largest values in scores, from largest to smallest.

  • Leave scores itself exactly as it was — the app still needs the original order.
  • Repeated scores count separately: if two players both scored 90 and both make the cut, 90 appears twice.
  • k is a whole number, 0 or more. If k is bigger than the number of scores, return all of them, largest first.

For example, top_k([42, 17, 88, 63, 5], 3) returns [88, 63, 42].

Examples

scores = [42, 17, 88, 63, 5]
k = 3
returns [88, 63, 42]
scores = [7, 3, 7, 1, 9]
k = 3
returns [9, 7, 7]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run