Exercice 91 ***
Écrire un algorithme python qui détermine la liste des mots de longueurs maximales communs à deux chaines textes T1 et T2.
Exemple si T1 = 'Python created by Guidorossum is open source programming language. Python was created on 1991'
et T2 = "Python created by Guidorossum is the most popular programming language Guidorossum", l'algorithme renvoie la liste:
1 |
['Guidorossum', 'programming'] |
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# coding: utf-8 #création d'une méthode qui détermine la liste des mots communs en T1 et T2 def commonWords(T1 , T2): # # convertir les textes en listes listWords_in_T1 = T1.split() listWords_in_T2 = T2.split() # initialisation de la liste des mots communs listCommon_words = [] for word in listWords_in_T1 : if word in listWords_in_T2 and word not in listCommon_words: listCommon_words.append(word) return listCommon_words # Création d'une méthode qui détermine le mot commun avec une longueur maximale def commonMax(T1, T2): listCommon_words = commonWords(T1 , T2) # intitialisation de la longueur maximale maxLength = 0 for word in listCommon_words: if len(word) >= maxLength: maxLength = len(word) # recherche des mots communs de longueurs maximales listCommonWordMax = [] for word in listCommon_words: if len(word) == maxLength: listCommonWordMax.append(word) return listCommonWordMax T1 = "Python created by Guidorossum is open source programming language. Python was created on 1991" T2 = "Python created by Guidorossum is the most popular programming language Guidorossum" print(commonMax(T1 , T2)) # La sortie est: ['Guidorossum', 'programming'] |
Younes Derfoufi
CRMEF OUJDA
Acheter sur Très Facile !
1 thought on “Solution Exercice 91: liste des mots de longueurs maximales communs à deux chaines texte python”