Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-031

Kraft Inequality Check

Problem

Return True if the given codeword lengths satisfy the Kraft inequality:

sum(2^(-l_i)) <= 1

Use a tolerance of 1e-9 when comparing, so exactly complete codes still pass.

Hint

A complete binary prefix code has sum exactly 1.

Test cases

case 1
in: [[1,2,3,3]]
expected: true
case 2
in: [[1,1]]
expected: true
case 3
in: [[1,2]]
expected: true
case 4
in: [[1,1,1]]
expected: false
Starter codePython
def kraft_inequality_holds(lengths):
    # Your code here
    pass

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