Created
November 29, 2017 10:38
-
-
Save apettinen/2ed1d8de7e39ec3e05066a3749c5a555 to your computer and use it in GitHub Desktop.
Simple entropy calculation of a string
This file contains 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/python | |
""" | |
Simple entropy calculation of a string | |
""" | |
from __future__ import division | |
from math import log | |
from collections import Counter | |
def get_entropy(s): | |
return -sum(i/len(s) * log(i/len(s),2) for i in Counter(s).values()) | |
def main(): | |
""" main function """ | |
mys = str(raw_input('Please enter a string: ')) | |
print('Entropy of string is: ' + str(get_entropy(mys))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment