Skip to content

Instantly share code, notes, and snippets.

@apettinen
Created November 29, 2017 10:38
Show Gist options
  • Save apettinen/2ed1d8de7e39ec3e05066a3749c5a555 to your computer and use it in GitHub Desktop.
Save apettinen/2ed1d8de7e39ec3e05066a3749c5a555 to your computer and use it in GitHub Desktop.
Simple entropy calculation of a string
#!/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