Shannon's Entropy is the "amount of information" in a variable. It translates to the amount of storage (e.g. number of bits) required to store the variable, which can intuitively be understood to correspond to the amount of information in that variable.
Write a function that returns the shannon entropy for a given string.
import math
def shannon_entropy(s):
symbols = list(s)
unique_symbols = set(s)
M = float(len(s))
entropy_list = []
for x in unique_symbols:
n_i = symbols.count(x)
P_i = n_i / M
entropy_i = P_i * (math.log(P_i, 2))
entropy_list.append(entropy_i)
sh_entropy = -(sum(entropy_list))
return sh_entropy
import codewars_test as test
from solution import shannon_entropy
@test.describe("Solution tests")
def test_group():
@test.it("Should correctly calculate Shannon entropy")
def _():
test.assert_equals(round(shannon_entropy("Lorem ipsum"),5), 3.27761)
test.assert_equals(round(shannon_entropy("110010111001011100101110010111001011100101"),5), 0.98523)
test.assert_equals(round(shannon_entropy("0000000"),5), 0)