11 lines
238 B
Python
11 lines
238 B
Python
import math
|
|
from collections import Counter
|
|
|
|
def entropy(s):
|
|
p, lns = Counter(s), float(len(s))
|
|
return -sum( count/lns * math.log(count/lns, 2) for count in p.values())
|
|
|
|
with open(__file__) as f:
|
|
b=f.read()
|
|
|
|
print(entropy(b))
|