Skip to content

Instantly share code, notes, and snippets.

@joneskoo
Created October 29, 2011 06:10
Show Gist options
  • Save joneskoo/1324161 to your computer and use it in GitHub Desktop.
Save joneskoo/1324161 to your computer and use it in GitHub Desktop.
Entropy calculation
#!/usr/bin/env python3
import math
def H(data):
entropy = 0
if not data:
return entropy
for x in range(256):
p_x = float(data.count(chr(x))/len(data))
if p_x > 0:
entropy += -p_x*math.log(p_x, 2)
return entropy
strings = [
"dLLGIsy9Cn0avbjJRyMP7yoL",
"gFnFcmVm8YT04uMmjafu28Cr"
]
for s in strings:
print("{:3.3f} x {} = {:.1f}".format(H(s), len(s), len(s)*H(s)))
# 4.189 x 24 = 100.5
# 4.137 x 24 = 99.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment