Problem 284047 · hard · Level 02 Linear Data Structures

Medal Standings

py-sort-key · py-join · py-enumerate · ranking

At a games, every medal awarded is recorded as a pair (country, medal), where medal is "gold", "silver" or "bronze". Write standings(results) that returns the medal table as one string, one line per country that won at least one medal:

<rank>. <country> <gold>-<silver>-<bronze>
  • Countries are ordered by gold medals, then silver, then bronze (more is better at each step); countries with exactly the same three counts are listed alphabetically.
  • Countries with the same three counts share a rank, and the ranks after them are skipped: two countries tied at rank 2 are followed by rank 4.
  • Lines are separated by "\n", with no newline at the end. With no results, return "".

Examples

Input:  results = [("NOR", "gold"), ("ITA", "silver"), ("NOR", "bronze"), ("KEN", "gold"),
                   ("ITA", "gold"), ("KEN", "bronze"), ("FIN", "silver")]
Output:
1. ITA 1-1-0
2. KEN 1-0-1
2. NOR 1-0-1
4. FIN 0-1-0

ITA, KEN and NOR each have one gold; ITA's silver puts it first. KEN and NOR have identical counts, so they share rank 2 in alphabetical order, and FIN is 4th.

Constraints

  • 0 <= len(results) <= 10**5
  • Country codes are non-empty strings of capital letters.

Goals

  • Sort by several fields, some descending and one ascending, with a single tuple key
  • Give tied entries the same rank and skip the ranks they use up
  • Produce a multi-line report by joining formatted lines
Starting Python…