diff --git a/Chapter_3/ex3.py b/Chapter_3/ex3.py index 845ddf297e0c159d31c7eccb5e000969b3344397..bf99c0da2667a4dc45c12c95f4fd8e68283fe452 100644 --- a/Chapter_3/ex3.py +++ b/Chapter_3/ex3.py @@ -1,4 +1,3 @@ - txt="Les objectifs de Python en font un langage pedagogique ideal.C'est un langage general-purpose:il s'adapte a toutes les applications" seperatorList = " ,.;-'\":\n/\\?+()’!°" @@ -22,3 +21,25 @@ while len(txt) > 0: txt="" print("Il y a {0} mots dans la phrase.".format(n_words)) + +# solution alternative sans utiliser "find" +# moins optimale sur de grands textes + +in_word = False +words = [] +curword = "" + +for letter in txt: + if letter in separators: + if in_word: + words.append(curword) + curword = "" + in_word = False + else: + in_word = True + curword += letter + +if in_word: + words += curword + +print("il y a", len(words), "mot(s)") \ No newline at end of file