Rank leaderboard scores so ties share a place
Model leaderboards print a place next to each score. When two models tie, they should share the same place, and the next model skips ahead, like 1, 2, 2, 4.
Write rank_scores(scores) for a 1D list or array of numbers. Return an int array of the same length where the highest score gets rank 1. Tied scores get the smallest rank of their group. The rank of a score equals 1 plus the number of scores strictly greater than it.
def rank_scores(scores)Example
rank_scores([70, 90, 80, 90]) is [4, 1, 3, 1].
Understand the idea
Competition ranking is the leaderboard style where tied scores share a place and the next place is skipped, like 1, 1, 3. A neat way to see it: a score's rank is 1 plus how many scores are strictly greater than it. That definition handles ties automatically. Plain argsort gives each item a different position even when scores tie, which is the classic mistake. Worked example: scores [5, 9, 9, 7]. For 5, three scores are greater, so rank 4. Each 9 has none greater, so rank 1. For 7, two scores are greater, so rank 3. Result [4, 1, 1, 3]. One loop-free way is to sort the scores descending and use np.searchsorted on the negated sorted array to count how many are strictly larger, or compare every pair with broadcasting for small inputs. Rule: rank_i = 1 + count of j with score_j > score_i, returned as ints. Model leaderboards and evaluation reports use this ranking.