All problems
Weighted Mean
Problem
Return sum(w * x) / sum(w) for paired values and weights. Return 0.0 if values is empty or the total weight is zero. Pairs are zipped, so extra unmatched elements are ignored.
Hint
Accumulate the weighted sum and the weight total in one pass.
Test cases
case 1
in: [[1,2,3],[1,1,1]]
expected: 2
case 2
in: [[80,90],[0.25,0.75]]
expected: 87.5
case 3
in: [[2,4,6],[3,0,1]]
expected: 3
case 4
in: [[10],[0]]
expected: 0
case 5
in: [[5,5],[2,2]]
expected: 5
Starter codePython
def weighted_mean(values, weights):
# Your code here
pass5 test cases · run your code against them in the editor