All problems
Run-Length Encoded Entropy
Problem
Run-length encode s and return the empirical Shannon entropy in bits of the run-length values, where each run contributes one sample. Return 0.0 for the empty string. Compute the runs first, then take the entropy of their value distribution.
Hint
A constant run-length distribution means the run-length code carries no information.
Test cases
case 1
in: ["aaabbbcc"]
expected: 0.9182958340544896
case 2
in: ["aaabbc"]
expected: 1.584962500721156
case 3
in: ["aaaa"]
expected: 0
case 4
in: ["aabbaabb"]
expected: 0
case 5
in: ["abab"]
expected: 0
Starter codePython
import math
def rle_entropy(s):
# Your code here
pass5 test cases · run your code against them in the editor