## Intro :

bestiaire = {"chien" : 4, "oiseau" : 2, "cafard" : 6, "crocodile" : 4, "araignée" : 8}
mondico = {"premier" : 1, "deuxième" : "Bonjour !", 3 : True}

nouveau_bestiaire = {}
nouveau_bestiaire["fourmi"] = 6
nouveau_bestiaire["serpent"] = 0
nouveau_bestiaire["mille-pattes"] = "trop"
print(nouveau_bestiaire)
mondico["4ème"] = 7
print(mondico)
nouveau_bestiaire["mille-pattes"] = "au plus 1306"
print(nouveau_bestiaire)


## Exercice 1

pokemon = {"carapuce" : 8, "salameche" : 9, "bulbizarre" : 10}
pokemon["pikachu"] = 7
pokemon_2 = {"kaiminus" : 8, "héricendre" : 10, "germignon" : 9}
starter = {"1ere génération" : pokemon, "2ème génération" : pokemon_2}
print(starter["2ème génération"]["kaiminus"])
#print(starter["1ère génération"]["kaiminus"]) # renvoie une erreur puisque le dictionnaire pokemon_2 ne contient pas la clé "kaiminus"


## Exercice 2
pokemon_1 = {'bulbizarre': 10, 'herbizarre': 10, 'florizarre': 10, 'salamèche': 9, 'reptincel': 9, 'dracaufeu': 9, 'carapuce': 8, 'carabaffe': 9, 'tortank': 7, 'chenipan': 8, 'chrysacien': 10, 'papilusion': 10, 'aspicot': 7, 'coconfort': 9, 'dardagnan' : 9, 'roucool': 7, 'roucoups': 8, 'roucarnage': 10, 'rattata': 7, 'rattatac': 8, 'piafabec': 8, 'rapasdepic': 10, 'abo': 3, 'arbok': 5, 'pikachu': 7, 'raichu': 6, 'sabelette': 9, 'sablaireau': 10, 'nidoran': 7, 'nidorina': 8, 'nidoqueen': 9 , 'nidorino': 8, 'nidoking': 8, 'mélofée': 7, 'mélodelfe': 9, 'goupix': 6, 'feunard': 7, 'rondoudou': 9, 'grodoudou': 9, 'nosferapti': 10, 'forferalto': 10, 'mystherbe': 9, 'ortide': 6, 'rafflesia': 9, 'paras': 5, 'parasect': 8, 'mimitoss': 8, 'aéromite': 8, 'taupiqueur': 10, 'triopikeur': 10, 'miaouss': 7, 'persian': 7, 'psykokwak': 9, 'akwakwak': 8, 'férosinge': 9, 'colossinge': 10, 'caninos': 7, 'arcanin': 7, 'ptitard': 7, 'têtarte': 7, 'tartard': 7, 'abra': 4, 'kadabra': 7, 'alakazam': 8, 'machoc': 6, 'machopeur': 9, 'mackogneur': 10, 'chétiflor': 9, 'boustiflor': 10, 'empiflor': 8, 'tentacool': 9, 'gravalanch': 10, 'grolem': 6, 'ponyta': 6, 'galopa': 6, 'ramoloss': 8, 'flagados': 8, 'magnéti': 7, 'magnéton': 8, 'canarticho': 10, 'doduo': 5, 'dodrio': 6, 'otaria': 6, 'lamantine': 9, 'tadmorv': 7, 'grotadmorv': 10, 'kokiyas': 7, 'crustabri': 9, 'fantominus': 10, 'spectrum': 8, 'ectoplasma': 10, 'onix': 4, 'soporifik': 9, 'hypnomade': 9, 'krabby': 6, 'krabboss': 8, 'voltorbe': 8, 'électrode': 9, 'nœunœuf': 7, 'noadkoko': 8, 'osselait': 8, 'ossatueur': 9, 'kicklee': 7, 'tygnon': 6, 'excelangue': 10, 'smogo': 5, 'smogogo': 7, 'rhinocorne': 10, 'rhinoféros': 10, 'leveinard': 9, 'saquedeneu': 10, 'kangourex': 9, 'hypotrempe': 10, 'hypocéan': 8, 'poissirène': 10, 'poissiroy': 9, 'stari': 5, 'staross': 7, 'm.mime': 6, 'insécateur': 10, 'lippoutou': 9, 'élektek': 7, 'magmar': 6, 'scarabrute': 10, 'tauros': 6, 'magicarpe': 9, 'léviator': 8, 'lokhlass': 8, 'métamorph': 9, 'évoli': 5, 'aquali': 6, 'voltali': 7, 'pyroli': 6, 'porygon': 7, 'amonita': 7, 'amonistar': 9, 'kabuto': 6, 'kabutops': 8, 'ptéra': 5, 'ronflex': 7, 'artikodin': 9, 'électhor': 8, 'sulfura': 7, 'minidraco': 9, 'draco': 5, 'dracolosse': 10, 'mew': 3, 'mewtwo': 6}


def noms_en_n(pokemons, n_lettres):
    L = []
    for clé in pokemons:
        if pokemons[clé] == n_lettres:
            L.append(clé)
    return L

def starters_en_n(starters, n_lettres):
    L = []
    for clé in starters:
        L = L + noms_en_n(starters[clé], n_lettres)
    return L


## Exercice 3


def stats_lettres(séquence):
    dico_comptage = {}
    for lettre in séquence :
        if lettre not in dico_comptage:
            dico_comptage[lettre] = 1
        else:
            dico_comptage[lettre] += 1
    return dico_comptage

def stats_bigrammes(sequence):
    dico_bigrammes = {}
    n = len(sequence)
    for i in range(n-1):
        bigramme = sequence[i:i+2]
        if bigramme not in dico_bigrammes:
            dico_bigrammes[bigramme] = 1
        else:
            dico_bigrammes[bigramme] += 1
    return dico_bigrammes

# Question 3 : k**n, et ici k=4
# Question 4 : le nombre de n-grammes de la séquence est N+1-n, donc on doit avoir N+1-k<4**n, c'est-à-dire N < 4**n + n-1

def compte_n_gram(sequence, n):
    dico = {}
    l = len(sequence)
    for i in range(l+1-n):
        ngramme = sequence[i:i+n]
        if ngramme not in dico:
            dico[ngramme] = 1
        else:
            dico[ngramme] += 1
    return dico


## Exercice 4

with open("germinal.txt") as f:
    livre = f.read()

# print(livre)

#1
print(len(stats_bigrammes(livre)))
#2
print(compte_n_gram(livre, 5)["prépa"])
#3
m = 0
trig = compte_n_gram(livre, 3)
for clé in trig :
    if trig[clé] > m:
        m = trig[clé]
        trigramme_max = clé
print(trigramme_max, m)


## Exercice 5

def update(d1, d2):
    for clé in d2:
        d1[clé] = d2[clé]

def union(d1, d2):
    d3 = {}
    for clé in d1:
        d3[clé] = d1[clé]
    for clé in d2:
        d3[clé] = d2[clé]
    return d3

"""La différence de ces deux fonctions est que la première procède par EFFET DE BORD en modifiant d1, alors que la seconde renvoie un troisième dictionnaire sans rien modifier aux arguments."""
