All problems
Entropy of Letter Counts
Problem
Given a dictionary mapping letters to counts, normalize the counts into probabilities and return the Shannon entropy in bits:
H = -sum(p * log2(p)), p = count / total
Skip zero counts and return 0.0 when the total count is zero.
Hint
The counts only matter through their ratios, so any normalization works.
Test cases
case 1
in: [{"a":1,"b":1}]
expected: 1
case 2
in: [{"a":3,"b":1}]
expected: 0.8112781244591328
case 3
in: [{"a":1,"b":2,"c":3,"d":4}]
expected: 1.8464393446710154
case 4
in: [{"a":10}]
expected: 0
case 5
in: [{"a":0,"b":4}]
expected: 0
Starter codePython
import math
def letter_entropy(counts):
# Your code here
pass5 test cases · run your code against them in the editor