Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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