Drill

ProblemsPython › reporting

The middle of a set of readings

mediumreportingPython

A latency report quotes the median rather than the mean, because one slow request should not move the headline number.

median_value(values: list<int>) → float

Solve it in the editor →

Where you start

def median_value(values: list[int]) -> float:
    

Worked examples

CallResult
median_value([3, 1, 2])2.0
median_value([1, 2, 3, 4])2.5
median_value([7])7.0
median_value([])0.0

Hint

After sorting, the two middles for an even count sit at n/2 - 1 and n/2.

Reference solution in Python
def median_value(values: list[int]) -> float:
    if not values:
        return 0.0
    s = sorted(values)
    n = len(s)
    mid = n // 2
    return float(s[mid]) if n % 2 == 1 else (s[mid - 1] + s[mid]) / 2

The same problem in another language

More reporting problems in Python