All problems
Expected Code Length
Problem
Given a probability distribution probs and matching integer codeword lengths, return the expected code length:
L = sum(p_i * l_i)
The two lists have the same length.
Hint
Weight each codeword length by the probability of the symbol it encodes.
Test cases
case 1
in: [[0.5,0.25,0.25],[1,2,2]]
expected: 1.5
case 2
in: [[0.5,0.5],[1,1]]
expected: 1
case 3
in: [[0.25,0.25,0.25,0.25],[2,2,2,2]]
expected: 2
case 4
in: [[0.1,0.9],[4,1]]
expected: 1.3
Starter codePython
def expected_code_length(probs, lengths):
# Your code here
pass4 test cases · run your code against them in the editor