Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-323

Code Efficiency Ratio

Problem

Compute the efficiency of a code as H / L, the ratio of the source entropy in bits to the expected codeword length. Probabilities are normalized before use and zero-probability symbols are skipped. Return 1.0 when L equals 0, which covers the single-symbol case.

Hint

Efficiency is entropy divided by expected length, never above 1 by Shannon's bound.

Test cases

case 1
in: [[0.25,0.25,0.25,0.25],[2,2,2,2]]
expected: 1
case 2
in: [[0.5,0.5],[1,2]]
expected: 0.6666666666666666
case 3
in: [[1],[0]]
expected: 1
case 4
in: [[1,3],[1,2]]
expected: 0.46358749969093305
Starter codePython
import math
def code_efficiency(probs, lengths):
    # Your code here
    pass

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