All problems
Sample Weight Normalization
Problem
Normalize sample weights so they sum to 1. If the sum is 0, return equal weights 1/n.
Empty input returns [].
Hint
Divide each weight by the total, falling back to uniform weights for an all-zero vector.
Test cases
case 1
in: [[1,1,2]]
expected: [0.25,0.25,0.5]
case 2
in: [[0,0,0]]
expected: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
case 3
in: [[2]]
expected: [1]
case 4
in: [[]]
expected: []
Starter codePython
def normalize_weights(weights):
# Your code here
pass4 test cases · run your code against them in the editor