## Exercice 1
# Question 1
def coordonnees_vers_indice(ordre, ligne, colonne):
    pass # TODO

def test_coords_vers_indice():
    assert(coordonnees_vers_indice(3, 0, 0) == 0)
    assert(coordonnees_vers_indice(3, 8, 8) == 80)
    assert(coordonnees_vers_indice(3, 1, 2) == 11)

# Décommentez la ligne suivante pour tester votre fonction
# test_coords_vers_indice()

# Question 2
def clique(sommets):
    pass # TODO

def test_clique():
    # Premier test
    liste_aretes = clique([5, 7])
    attendues = [(5, 7), (7, 5)]
    for arete in attendues:
        assert(arete in liste_aretes)
    # Deuxième test
    liste_aretes = clique([3, 6, 8])
    attendues = [(3, 6), (3, 8), (6, 3), (6, 8), (8, 3), (8, 6)]
    for arete in attendues:
        assert(arete in liste_aretes)

# Décommentez la ligne suivante pour tester votre fonction
# test_clique()

# Question 3
def ordre_vers_grille(ordre):
    pass # TODO

# Code fourni pour afficher un graphe
import subprocess

def liste_aretes_vers_PDF(liste_aretes):
    with open("graphe.gv", "w") as g:
        g.write("graph {\n")
        for (u, v) in liste_aretes:
            if u < v:
                g.write(f"  {u} -- {v};\n")
        g.write("}\n")
    with open("graphe.pdf", "w") as out:
        subprocess.run(["dot", "-Tpdf", "graphe.gv"], stdout=out)
    subprocess.run(["evince", "graphe.pdf"])

# Décommentez la ligne suivante pour visualiser votre graphe à l'ordre 2
# liste_aretes_vers_PDF(ordre_vers_grille(2)[1])

## Exercice 2

# Question 1
def complete(partiel,total):
    pass # TODO

def test_complete():
    # Exemple de coloriage total
    # d'une grille de sudoku d'ordre 2
    total = [2, 3, 1, 4,
             4, 1, 3, 2,
             3, 2, 4, 1,
             1, 4, 2, 3]
    # Un coloriage partiel associé
    partiel = [2, 0, 1, 0,
               4, 0, 0, 2,
               3, 2, 4, 0,
               1, 4, 2, 3]
    # Un test positif (ici la fonction doit renvoyer True)
    assert(complete(partiel, total))
    # Un coloriage partiel incompatible
    autre = [2, 0, 1, 3,
             4, 0, 0, 2,
             3, 2, 4, 0,
             1, 4, 2, 3]
    # Un test négatif (ici la fonction doit renvoyer False)
    assert(not complete(autre, total))
    # Un test d'erreur (ici la fonction doit échouer)
    try:
        complete(partiel, partiel)
    except:
        return
    assert(False) # La fonction testée n'a pas échoué comme elle aurait dû

# Décommentez la ligne suivante pour tester votre fonction
# test_complete()

# Question 2
def valide(graphe, coloriage):
    pass # TODO

def test_valide():
    graphe = ordre_vers_grille(2)
    exemple_valide = [2, 3, 1, 4,
                      4, 1, 3, 2,
                      3, 2, 4, 1,
                      1, 4, 2, 3]
    assert(valide(graphe, exemple_valide))
    invalide_ligne = [2, 3, 1, 4,
                      4, 1, 3, 2,
                      3, 2, 4, 3,
                      1, 4, 2, 1]
    assert(not valide(graphe, invalide_ligne))
    invalide_colonne = [2, 3, 1, 4,
                        4, 1, 3, 2,
                        3, 2, 4, 1,
                        4, 1, 2, 3]
    assert(not valide(graphe, invalide_colonne))
    invalide_bloc = [2, 3, 1, 4,
                     3, 2, 4, 1,
                     4, 1, 3, 2,
                     1, 4, 2, 3]
    assert(not valide(graphe, invalide_bloc))

# Décommentez la ligne suivante pour tester votre fonction
# test_valide()
