Dataclasses, enumerations and types · 10 / 15 Exercise Medium

Ranking with sortable dataclasses

Objective

Create a Python program with a sortable dataclass: with order=True, the objects are compared by their attributes, in the order in which they are declared.

Instructions

  • Define @dataclass(order=True) class Player with points: int and name: str, in that order.
  • Read an integer n and then n lines with the format name points.
  • Sort the players from highest to lowest with sorted(players, reverse=True). They are compared first by points and, if tied, by name.
  • Show the ranking, one line per player, with the format N. name (points), starting at 1.
Input
3
ana 50
luis 80
eva 65
Output
1. luis (80)
2. eva (65)
3. ana (50)

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise