Problem 255594 · easy · Level 02 Linear Data Structures

Pairing Names with Scores

py-sort-key · py-zip · sorting · stability

A quiz stores the players' names and their scores in two lists of the same length: scores[i] belongs to names[i]. Write ranking(names, scores) that returns the list of (name, score) pairs from the highest score to the lowest. Players with equal scores stay in the order in which they appear in the input.

Examples

Input:  names = ["ana", "ben", "chen", "dev"], scores = [7, 9, 7, 10]
Output: [("dev", 10), ("ben", 9), ("ana", 7), ("chen", 7)]

Input:  names = ["zed", "amy"], scores = [5, 5]
Output: [("zed", 5), ("amy", 5)]
Explanation: A tie keeps the input order; it is not broken by name.

Constraints

  • 0 <= len(names) == len(scores) <= 10**5
  • Scores are integers; names are distinct strings.

Goals

  • Pair two parallel lists with `zip`
  • Sort pairs by one field with `key=` and `reverse=True`
  • Rely on the stability of `sorted` to keep ties in their original order
Starting Python…