Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-024

Percentile (Linear Interpolation)

Problem

Return the p-th percentile (0 <= p <= 100) using linear interpolation between closest ranks: rank = p/100 * (n-1), then interpolate between the sorted values at floor(rank) and ceil(rank). Return the single value when n = 1 and 0.0 for empty input.

Hint

The interpolation fraction is rank minus its floor.

Test cases

case 1
in: [[1,2,3,4,5],50]
expected: 3
case 2
in: [[1,2,3,4],25]
expected: 1.75
case 3
in: [[10,20,30,40],75]
expected: 32.5
case 4
in: [[5],90]
expected: 5
case 5
in: [[1,2,3,4,5,6,7,8,9,10],90]
expected: 9.1
Starter codePython
def percentile(data, p):
    # Your code here
    pass

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