Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-114

Rank Transform with Average Ties

Problem

Rank values in ascending order, assigning tied values the average of their 1-based positions.

Return the rank list; empty input returns [].

Hint

Sort indices, find runs of equal values, and give each run the mean of its positions.

Test cases

case 1
in: [[3,1,2]]
expected: [3,1,2]
case 2
in: [[10,20,20,30]]
expected: [1,2.5,2.5,4]
case 3
in: [[5,5,5]]
expected: [2,2,2]
case 4
in: [[]]
expected: []
Starter codePython
def rank_transform(values):
    # Your code here
    pass

4 test cases · run your code against them in the editor