## L305_x : Kick off for L305
## SXD 305

'''Exercises on entropy'''


from cdi import * # hay que poner cdi.py en un directorio
                  # que esté en el path de pyhon 

# 1. Study the functions in the section Information theory of cdi

# 2. Solve the exercises on T2.6

# Problems T2.6

e1=entropy(3*[1/6]+4*[1/8])
e2=entropy(2*[1/4]+6*[1/12])

print(round((e1,e2),3))

e1 = 26*L2(10)
e2 = 10*L2(26)

print(round((e1,e2),3))

 
# 3. Compute the entropy of monogram English, with and without space

F={'a': 651738, 'b':  124248, 'c': 217339, 'd': 349835, 'e':1041442, 
   'f': 197881, 'g':  158610, 'h': 492888, 'i': 558094, 'j':   9033, 
   'k':  50529, 'l':  331490, 'm': 202124, 'n': 564513, 'o': 596302, 
   'p': 137645, 'q':    8606, 'r': 497563, 's': 515760, 't': 729357, 
   'u': 225134, 'v':   82903, 'w': 171272, 'x':  13692, 'y': 145984, 
   'z':   7836, '_': 1918182}

Z = [c for c in F.keys()]

f_ = [F[c] for c in Z]

f = [F[c] for c in Z if c != '_']

print(round(entropy(f_),3))
print(round(entropy(f),3))

# 4. Compute the entropy of the Morse code
M = {
'a': (1,1), 'b': (3,1), 'c': (2,2), 'd': (2,1), 'e':(1,0), 
   'f':(3,1), 'g':(1,2), 'h':(4,0), 'i': (2,0), 'j':(1,3), 
   'k':(1,2), 'l':(3,1), 'm':(0,2), 'n': (1,1), 'o':(0,3), 
   'p':(2,2), 'q':(1,3), 'r':(2,1), 's': (3,0), 't':(0,1), 
   'u':(2,1), 'v':(3,1), 'w':(1,2), 'x': (2,2), 'y':(1,3), 
   'z':(2,2)
}

f_space = F['_']
f_dot = sum(F[c]*M[c][0] for c in M)
f_dash = sum(F[c]*M[c][1] for c in M)

E = [f_dot, f_dash]
E_ = [f_dot, f_dash, f_space]

e = entropy(E)
e_ = entropy(E_)

print(round(e,3))
print(round(e_,3))
























