Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-275

Arithmetic Coding Bits Needed

Problem

After arithmetic coding, the final interval has some width. Return the number of bits needed to distinguish a point in that interval, which is ceil(-log2(width)). Assume 0 < width <= 1, so an interval of width 1.0 needs 0 bits.

Hint

Halving the interval width adds exactly one bit to the description.

Test cases

case 1
in: [0.125]
expected: 3
case 2
in: [0.3]
expected: 2
case 3
in: [1]
expected: 0
case 4
in: [0.01]
expected: 7
case 5
in: [0.5]
expected: 1
Starter codePython
import math
def arithmetic_bits_needed(width):
    # Your code here
    pass

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