Skip to content
DeepForgeby svx
All problems
EasyStatisticsst-009

Harmonic Mean

Problem

Return the harmonic mean n / sum(1/x). Return 0.0 if data is empty, contains a zero, or the reciprocal sum is zero.

Hint

Sum the reciprocals first, then divide the count by that sum.

Test cases

case 1
in: [[1,2,4]]
expected: 1.7142857142857142
case 2
in: [[2,6]]
expected: 3
case 3
in: [[10]]
expected: 10
case 4
in: [[1,0,3]]
expected: 0
case 5
in: [[4,4,4]]
expected: 4
Starter codePython
def harmonic_mean(data):
    # Your code here
    pass

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