# Demo entry 6642265

1

Submitted by anonymous on Sep 22, 2017 at 17:17
Language: Python 3. Code size: 1.4 kB.

```import operator
import math

def entropy(distribution):
res = 0.0
for i in distribution:
res += i * math.log(i, 2)
return(-res)

dic = {}
count = 0
for i in range(1,21):
try:
if i <10:
f=open(r'C:\Users\Yuchen Liu\Desktop\CSC659\HW2\fc2\A%s'%('0'+str(i)),"r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split('\t'))
f.close()

for i in result:
count +=1
if i in dic.keys():
dic[i] = dic[i]+1
else:
dic[i] = 1
else:
f=open(r'C:\Users\Yuchen Liu\Desktop\CSC659\HW2\fc2\A%s'%(str(i)),"r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split('\t'))
f.close()

for i in result:
count +=1
if i in dic.keys():
dic[i] = dic[i]+1
else:
dic[i] = 1
except:
pass

for k,v in dic.items():
dic[k] =dic[k]/count

s_list = sorted(dic.items(), key=operator.itemgetter(1),reverse =True)

distribution = []
for i in s_list:

distribution.append(i)

entropy = entropy(distribution)

print (entropy)
```

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.