Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-004

Binary Entropy Function

Problem

Compute the binary entropy function in bits:

H(p) = -p * log2(p) - (1 - p) * log2(1 - p)

Return 0.0 for p = 0 or p = 1.

Hint

The function is symmetric: H(p) = H(1 - p).

Test cases

case 1
in: [0]
expected: 0
case 2
in: [0.5]
expected: 1
case 3
in: [1]
expected: 0
case 4
in: [0.25]
expected: 0.8112781244591328
case 5
in: [0.9]
expected: 0.4689955935892811
Starter codePython
import math
def binary_entropy(p):
    # Your code here
    pass

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