Created
October 29, 2011 06:10
-
-
Save joneskoo/1324161 to your computer and use it in GitHub Desktop.
Entropy calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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