#!/usr/bin/python3
# coding=utf-8
# La première ligne sert pour python (et ipython) 2, qui n'aiment pas
# les caractères non ASCII.

# Dans ipython :
# run seance6.py

def word_to_frequency_table (word):
    length = len (word)
    d = dict ({})
    for c in word:
        if c in d:
            d[c] = d[c] + (1.0 / length)
        else:
            d[c] = 1.0 / length
    return d

def update_frequency_table_with_word (ft, word):
    for c in word:
        if c in ft:
            ft[c] = ft[c] + 1
        else:
            ft[c] = 1

def sum_of_list_elements (list):
    s = 0
    for e in list:
        s = s + e
    return s

def words_to_frequency_table (words):
    d = dict ({})
    for word in words:
        update_frequency_table_with_word (d, word)
    # Convert from character count to character frequency:
    character_no = sum_of_list_elements (d.values ())
    for c in d:
        d[c] = d[c] / float (character_no)
    return d

def dictionary_to_list_of_inverted_pairs (d):
    r = []
    for k in d:
        r.append ((d[k], k))
    return r

# Recyclé
def unzip (a):
    r1 = []
    r2 = []
    for e in a:
        r1.append (e[0])
        r2.append (e[1])
    return r1, r2

def words_to_letters_sorted_by_frequency (words):
    frequency_table = words_to_frequency_table (words)
    list_of_frequency_character_pairs = dictionary_to_list_of_inverted_pairs (frequency_table)
    list_of_frequency_character_pairs.sort () # L'ordre lexicographique est utile !
    list_pair = unzip (list_of_frequency_character_pairs)
    return list_pair[1]
