Commit a8aa0aef by Mathieu-Duran

### Algo Levenshtein

parent 61286751
 # -*- coding: utf8 -*- import sys #Calcul de la distance de Levenshtein def levenshtein(word1,m,word2,n): print("Mot 1: " + word1 + " | Taille : " + str(m)) print("Mot 2: " + word2 + " | Taille : " + str(n)+"\n") #Création du tableau à 2 dimensions lev = [[0] * (n+1) for i in range(m+1)] #Calcul for i in range(1,m+1): lev[i][0]=i for j in range(1,n+1): lev[0][j] = j for i in range(1,m+1): for j in range(1,n+1): if word1[i-1] != word2[j-1]: lev[i][j]=min(lev[i-1][j]+1,lev[i][j-1]+1,lev[i-1][j-1]+1) else: lev[i][j]=min(lev[i-1][j]+1,lev[i][j-1]+1,lev[i-1][j-1]) #Affichage console for x in range(0,m+1): print(lev[x]) print("\nDistance de Levenshtein = "+str(lev[m][n])) #Retour de la distance de Levenshtein return lev[m][n] #Prend les 2 premiers texte en argument sinon demande à l'utilisateur d'insérer 2 textes if (len(sys.argv)>=3): levenshtein(sys.argv[1],len(sys.argv[1]),sys.argv[2],len(sys.argv[2])) else: word1 = input("Premier mot : ") word2 = input("Second mot : ") levenshtein(word1,len(word1),word2,len(word2))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!