## A324-boixaderas

'''
Finding the average time units needed for the 
transmission of one character using the Morse code.
'''

## 0. Data

# Frequencies of characters, including 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}
   

# Dictionary M whose entries have the form x:(a,b), where
# x is a character, and a, b are the number of dots and dashes
# in the Morse encoding of x. Note the entry '_':(1,0)

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), '_':(1,0)
}

##  1. 
''' Define a function time_units(x) that gives the
    the number of time units needed fot the transmission of x.
    Add comments that document your reasoning.
'''
# point + space, 3*dash + space, 3 between letters - 1 (because I already counted a space at the end of the letter)
# Therefore, 2*point+4*dash+2
def time_units(x):
  return 2*M[x][0]+4*M[x][1]+2


##  2. 
''' Produce an expression that gives the (weighted) average
    A of time units required to transmit a character.
'''
A = sum(F[f]*time_units(f) for f in F.keys()) / sum(F.values())

print("Weighted Average:", A)


##  3. 
''' Which characters have maximum (minimum) time-length?
'''  
L = [time_units(f) for f in F.keys()]
Max_L = max(L)
Min_L = min(L)

MAX = [f for f in F.keys() if time_units(f) == Max_L]
MIN = [f for f in F.keys() if time_units(f) == Min_L]

print("Maximum time-length characters:", MAX)
print("Minimum time-length characters:", MIN)


##  4.
''' Compare the average A with the average time-length 
    of Huffman encoding assuming that a bit transmission 
    amounts to a time unit. 
'''
print("From A317 we got that Weighted average of bits for Huffman enconding was ~4.12")
print("Weighted average of time units of morse code: ", A)
print("Huffman encoding it's a more efficient encoding")
