Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
19
Task/Entropy/Python/entropy-3.py
Normal file
19
Task/Entropy/Python/entropy-3.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
def Entropy(text):
|
||||
import math
|
||||
log2=lambda x:math.log(x)/math.log(2)
|
||||
exr={}
|
||||
infoc=0
|
||||
for each in text:
|
||||
try:
|
||||
exr[each]+=1
|
||||
except:
|
||||
exr[each]=1
|
||||
textlen=len(text)
|
||||
for k,v in exr.items():
|
||||
freq = 1.0*v/textlen
|
||||
infoc+=freq*log2(freq)
|
||||
infoc*=-1
|
||||
return infoc
|
||||
|
||||
while True:
|
||||
print Entropy(raw_input('>>>'))
|
||||
Loading…
Add table
Add a link
Reference in a new issue