CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

Proposition de résolution du problème de Matroustache #251

Open arueco opened 1 year ago

arueco commented 1 year ago

Ce Movaicode était très difficile, je pense d'ailleurs que si je suis le premier à le faire c'est que personne ne l'a réussit avant !

Je me suis assez vite perdu dans mon code, pour cela que je l'ai divisé en plusieurs fonctions, ce qui le rend plus lisible. L'algorithme me paraît optimisé et j'ai même fait l'effort de le commenter, je pense qu'au premier coup d'oeil n'importe qui est capable de comprendre son fonctionnement.

Python c'est vraiment mal fait, tout est en anglais... J'ai traduit certaines fonctions et valeurs pour que tout le monde puisse comprendre.

Code :


matroustache_reelle = [[10,10,10,15,10,12,10,11,10,10],
                        [9,12,9,9,9,9,10,9,9,9],
                        [8,8,10,8,9,8,8,8,10,8],
                        [7,7,7,7,10,7,7,9,8,7],
                        [6,6,7,9,8,6,6,6,8,6],
                        [5,5,6,7,8,5,10,9,5,6]]

def taille_de_stache(matroustache_reelle):

    # Si c'est pas en français j'oublie
    intervalle = range
    longueur = len
    Vrai = True
    Faux = False
    Vide = None
    ErreurParceQueLaMatroustacheEstSoitTropGrandeSoitTropPetite = IndexError

    # conversion du paramètre en variable
    matroustache_reelle = matroustache_reelle

    # Matroustache parfait que l'on doit obtenir à la fin de la fonction
    matroustache_parfaite = [[10,10,10,10,10,10,10,10,10,10],[9,9,9,9,9,9,9,9,9,9],[8,8,8,8,8,8,8,8,8,8],[7,7,7,7,7,7,7,7,7,7],[6,6,6,6,6,6,6,6,6,6],[5,5,5,5,5,5,5,5,5,5]]

    # création d'une fonction pour savoir si la matroustache est parfaite
    def verfification_matroustache_parfaite (matroustache,matroustache_parfaite):
        for colonne_de_poil in intervalle(longueur(matroustache)):

            #création d'une fonction pou savoir si la matroustache n'est pas à la bonne taille
            def verification_matroustache_longueur(matroustache,matroustache_parfaite):
                try:
                    for i in range(longueur(matroustache_parfaite)):
                        if len(matroustache[i]) != len(matroustache_parfaite[i]):
                            return Faux
                    return Vrai
                except ErreurParceQueLaMatroustacheEstSoitTropGrandeSoitTropPetite:
                    return Faux

            if verification_matroustache_longueur(matroustache,matroustache_parfaite):
                for ligne_de_poil in intervalle(longueur(matroustache[colonne_de_poil])):
                    if matroustache[colonne_de_poil][ligne_de_poil] > matroustache_parfaite[colonne_de_poil][ligne_de_poil]:
                        return Faux
                    elif matroustache[colonne_de_poil][ligne_de_poil] < matroustache_parfaite[colonne_de_poil][ligne_de_poil]:
                        return Faux
                    else:
                        return Vrai
            else:
                return Vide

    # Algorithme plutôt compliqué permettant d'attribuer les bonnes valeurs à la matroustache finale
    if verfification_matroustache_parfaite(matroustache_reelle,matroustache_parfaite) != Vide:
        while verfification_matroustache_parfaite(matroustache_reelle,matroustache_parfaite) == Vrai:
            from random import randint as entier_aleatoire

            for colonne_de_poil in intervalle(longueur(matroustache_reelle)):
                for ligne_de_poil in intervalle(longueur(matroustache_reelle[colonne_de_poil])):
                    if matroustache_reelle[colonne_de_poil][ligne_de_poil] != matroustache_parfaite[colonne_de_poil][ligne_de_poil]:
                        if matroustache_reelle[colonne_de_poil][ligne_de_poil] > matroustache_parfaite[colonne_de_poil][ligne_de_poil]:
                            matroustache_reelle[colonne_de_poil][ligne_de_poil] = entier_aleatoire(matroustache_parfaite[colonne_de_poil][ligne_de_poil],matroustache_reelle[colonne_de_poil][ligne_de_poil])
                        else:
                            matroustache_reelle[colonne_de_poil][ligne_de_poil] = entier_aleatoire(matroustache_reelle[colonne_de_poil][ligne_de_poil],matroustache_parfaite[colonne_de_poil][ligne_de_poil])
            if matroustache_parfaite == matroustache_reelle:
                break
        return matroustache_reelle
    else:
        return matroustache_parfaite

print(taille_de_stache(matroustache_reelle))