scrabble = {
    'A':1,
    'B':3,
    'C':3,
    'D':2,
    'E':1,
    'F':4,
    'G':2,
    'H':4,
    'I':1,
    'J':8,
    'K':10,
    'L':1,
    'M':2,
    'N':1,
    'O':1,
    'P':3,
    'Q':8,
    'R':1,
    'S':1,
    'T':1,
    'U':1,
    'V':4,
    'W':10,
    'X':10,
    'Y':10,
    'Z':10
    }

code_morse = {
    'A':'- ---',
    'B':'--- - - -',
    'C':'--- - --- -',
    'D':'--- - -',
    'E':'-',
    'F':'- - --- -',
    'G':'--- --- -',
    'H':'- - - -',
    'I':'- -',
    'J':'- --- --- ---',
    'K':'--- - ---',
    'L':'- --- - -',
    'M':'--- ---',
    'N':'--- -',
    'O':'--- --- ---',
    'P':'- --- --- -',
    'Q':'--- --- - ---',
    'R':'- --- -',
    'S':'- - -',
    'T':'---',
    'U':'- - ---',
    'V':'- - - ---',
    'W':'- --- ---',
    'X':'--- - - ---',
    'Y':'--- - --- ---',
    'Z':'--- --- - -',
    }

message = "--- - --- -  --- --- ---  --- --- -  - -  ---  --- --- ---       -  - --- -  --- --- -  --- --- ---       - - -  - - ---  --- ---"




## Exercice 1

dictionnaire_de_prix = {
    "Beurre": 2.61,
    "Café": 5.29,
    "Céréales": 2.99,
    "Jus de fruits": 2.51,
    "Lait": 5.46,
    "Oeufs": 1.49,
    "Pâtes": 1.70,
    "Riz": 2.39
}


def disponibilite(produit, dictionnaire_de_prix):
    return produit in dictionnaire_de_prix

def prix_moyen(dictionnaire_de_prix):
    S = 0
    for produit, prix in dictionnaire_de_prix.items():
        S += prix
    return S / len(dictionnaire_de_prix)

def fourchette_de_prix(a, b, dictionnaire_de_prix):
    L = []
    for produit, prix in dictionnaire_de_prix.items():
        if a <= prix <= b:
            L.append(produit)
    return L

def prix_achats_1(panier, dictionnaire_de_prix):
    Total = 0
    for produit in panier:
        if disponibilite(produit, dictionnaire_de_prix):
            Total += dictionnaire_de_prix[produit]
    return Total

def prix_achats_2(panier, dictionnaire_de_prix):
    Total = 0
    for produit in panier.keys():
        if disponibilite(produit, dictionnaire_de_prix):
            Total += dictionnaire_de_prix[produit] * panier[produit]
    return Total


## Exercice 2

def occurrences(L):
    D = {}
    for e in L:
        if e in D:
            D[e] += 1
        else:
            D[e] = 1
    return D


## Exercice 3

def points_1(mot):
    points = 0
    for lettre in mot:
        points += scrabble[lettre]
    return points

def points_2(mot, lettres_compte_double):
    points = 0
    for i in range(len(mot)):
        lettre = mot[i]
        if i in lettres_compte_double:
            points += 2 * scrabble[lettre]
        else:
            points += scrabble[lettre]
    return points


## Exercice 4

def note(reponses_eleve, bonnes_reponses):
    points = 0
    for question, reponse in reponses_eleve.items():
        if reponse == bonnes_reponses[question]:
            points += 3
        else:
            points -= 1
    return points


## Exercice 5

def traduction_francais_morse(texte):
    code = ""
    for lettre in texte:
        if lettre == " ":
            code += 5 * " " # car on a ajouté 2 espaces après la lettre précédente
        else:
            code += code_morse[lettre] + 2 * " "
    return code[:-2]

def traduction_morse_francais(code):
    decode = {}
    for morse, lettre in code_morse.items():
        decode[lettre] = morse

    texte = ""
    mots_codes = code.split(7 * " ")
    for mot in mots_codes:
        lettres = mot.split(2 * " ")
        for lettre in lettres:
           texte += decode[lettre]
        texte += " "

    return texte[:-1]

print(traduction_morse_francais(message))


## Exercice 6

def occurrences_lettres(texte):
    D = {}
    for lettre in texte:
        if ord('A') <= ord(lettre) <= ord('Z'):
            L = lettre
        elif ord('a') <= ord(lettre) <= ord('z'):
            L = chr(ord(lettre)-32)
        else:
            L = ""
        if L in D and L != "":
            D[L] += 1
        elif L != "":
            D[L] = 1
    return D

def pourcentage_lettres(texte):
    D = occurrences_lettres(texte)
    S = 0
    for lettre, occurrence in D.items():
        S += occurrence
    P = {}
    for lettre, occurrence in D.items():
        P[lettre] = occurrence / S
    return P

def maximum(D):
    """
    Renvoie la clé ayant la valeur maximale dans un dictionnaire D constitué
    uniquement de valeurs positives.
    """
    M = 0
    for cle, valeur in D.items():
        if valeur > M:
            M = valeur
            record = cle
    return record

def tri_par_occurrences(D):
    L = []
    for i in range(len(D)):
        M = maximum(D)
        L.append(M)
        del D[M]
    return L
