runtime
278 / 500
question 278 Data Models & Typing · medium

Top scores, with type hints

A leaderboard shows the best results of a course quiz. Write top_n(scores, n=3) with type hints: it takes a dictionary from learner name to score and returns a list of (name, score) tuples for the n highest scores, highest first; equal scores are listed by name A–Z. A negative n is a ValueError. The hints are checked too.

Functiontop_n(scores: dict[str, float], n: int = 3) -> list[tuple[str, float]]
Returnsat most n tuples, highest score first; ties by name A–Z; fewer if there are fewer learners
RaisesValueError for n < 0
Examples
calltop_n({'Nora Garcia': 91.5, 'Jamal Walker': 78, 'Grace Walker': 91.5, 'Theo Williams': 64.25}, 2)returns[('Grace Walker', 91.5), ('Nora Garcia', 91.5)]
calltop_n({'Nora Garcia': 91.5, 'Jamal Walker': 78, 'Grace Walker': 91.5, 'Theo Williams': 64.25})returns[('Grace Walker', 91.5), ('Nora Garcia', 91.5), ('Jamal Walker', 78.0)]
calltop_n({'Ana Lopez': 55}, 3)returns[('Ana Lopez', 55.0)]

Loading your progress…

Top scores, with type hints — Python practice · Runtime