prog:python:palindrome
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
prog:python:palindrome [2020/04/06 09:03] – créée jbpuel | prog:python:palindrome [2020/04/16 13:35] (Version actuelle) – jbpuel | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Variations autour du palindrome ====== | ====== Variations autour du palindrome ====== | ||
+ | Télécharger le fichier : {{ : | ||
+ | Déterminer si une phrase fournie par l' | ||
- | < | + | * parcours classique de la chaîne, tel qu'on l' |
+ | * parcours de la chaîne mais la notation des indices est plus spécifique à python | ||
+ | * utiliser les fonction sur les chaînes de caractères de Python : comparaison de la chaîne et de son miroir | ||
+ | * fonction récursive | ||
+ | <WRAP centeralign> | ||
+ | == Voir le programme == | ||
+ | |||
+ | <code python> | ||
""" | """ | ||
Ligne 41: | Ligne 50: | ||
phrase = phrase.replace(" | phrase = phrase.replace(" | ||
return phrase | return phrase | ||
+ | |||
ph = input(" | ph = input(" | ||
ph = nettoyage(ph) | ph = nettoyage(ph) | ||
- | print (" | + | print(" |
i = 0 | i = 0 | ||
long = len(ph) | long = len(ph) | ||
Ligne 50: | Ligne 60: | ||
# Méthode 1 : parcours classique de la chaîne | # Méthode 1 : parcours classique de la chaîne | ||
- | while i< | + | while i < long/2: |
if (ph[i] == ph[long-1-i]): | if (ph[i] == ph[long-1-i]): | ||
pal = True | pal = True | ||
Ligne 56: | Ligne 66: | ||
pal = False | pal = False | ||
break | break | ||
- | i=i+1 | + | i = i+1 |
- | + | ||
if pal: | if pal: | ||
- | print (" | + | print(" |
else: | else: | ||
- | print ("Pas palindrome" | + | print(" |
# Méthode 2 : parcours de la chaîne, notation plus python | # Méthode 2 : parcours de la chaîne, notation plus python | ||
- | while i< | + | while i < long/2: |
if (ph[i] == ph[-1-i]): | if (ph[i] == ph[-1-i]): | ||
pal = True | pal = True | ||
Ligne 70: | Ligne 80: | ||
pal = False | pal = False | ||
break | break | ||
- | i=i+1 | + | i = i+1 |
- | + | ||
if pal: | if pal: | ||
- | print (" | + | print(" |
else: | else: | ||
- | print ("Pas palindrome" | + | print(" |
# Méthode 3 : comparaison de la chaîne et de son miroir, encore plus python | # Méthode 3 : comparaison de la chaîne et de son miroir, encore plus python | ||
ph2 = ph[::-1] | ph2 = ph[::-1] | ||
if ph2 == ph: | if ph2 == ph: | ||
- | print (" | + | print(" |
else: | else: | ||
- | print ("Pas palindrome" | + | print(" |
- | + | ||
# Méthode 4 : fonction récursive | # Méthode 4 : fonction récursive | ||
- | def palindrome (phrase): | + | def palindrome(phrase): |
if phrase[0] == phrase[-1]: | if phrase[0] == phrase[-1]: | ||
pal = True | pal = True | ||
if len(phrase) > 2: | if len(phrase) > 2: | ||
- | pal = palindrome (phrase[1: | + | pal = palindrome(phrase[1: |
else: | else: | ||
pal = False | pal = False | ||
return pal | return pal | ||
+ | |||
if palindrome(ph): | if palindrome(ph): | ||
- | print (" | + | print(" |
else: | else: | ||
- | print ("Pas palindrome" | + | print(" |
+ | | ||
</ | </ | ||
+ | </ |
prog/python/palindrome.1586163813.txt.gz · Dernière modification : 2020/04/06 09:03 de jbpuel