Problem 436568 · easy · Level 04 Non-Linear Data Structures

Race Results as Records

py-dataclasses · ordering · records

The timing system of a running club records each finish as a time in seconds, a bib number, the runner's name and their club. Names are typed in by volunteers and are sometimes misspelt, so the system identifies and orders finishes by time and bib only.

Write two classes:

  • Finish(time, bib, name, club=""):
    • repr shows every field as name=value: Finish(time=61.2, bib=7, name='Ana', club='');
    • two finishes are equal when their time and bib are equal (the name and club do not matter);
    • finishes compare with <, >, <=, >= by time, and by bib when the times are equal, so sorted, min and max put them in finishing order.
  • Results(race), holding the race name and a list finishes that starts empty for each race:
    • add(finish) appends a finish;
    • podium() returns the names of the (up to) three best finishes, best first;
    • clubs() returns a dictionary from each club to its number of finishes, with the clubs in alphabetical order;
    • repr shows both fields: Results(race='5k', finishes=[...]).

The tests compare repr strings and use ==, sorted, min and max on your objects. The setup's parkrun(n, seed) generates n finishes.

Examples

Input:  a, b, c = Finish(61.2, 7, "Ana"), Finish(59.8, 3, "Bo", "Harriers"), Finish(61.2, 2, "Cy")
        repr(sorted([a, b, c])), a == Finish(61.2, 7, "Anna", "Striders"), max(a, b, c).name
Output: ("[Finish(time=59.8, bib=3, name='Bo', club='Harriers'), Finish(time=61.2, bib=2, name='Cy', club=''), Finish(time=61.2, bib=7, name='Ana', club='')]",
         True, 'Ana')

Input:  r = Results("5k"); r.add(Finish(1500.0, 11, "Dee", "Harriers")); repr(r), repr(Results("10k"))
Output: ("Results(race='5k', finishes=[Finish(time=1500.0, bib=11, name='Dee', club='Harriers')])",
         "Results(race='10k', finishes=[])")

Constraints

  • Up to 5000 finishes per race; times are positive floats and bibs positive integers.

Goals

  • Replace a hand-written `__init__`, `__repr__`, `__eq__` and `<` with `@dataclass`
  • Choose which fields take part in comparisons with `field(compare=False)`
  • Give every object its own list with `field(default_factory=list)`
Starting Python…