def palindrome1 ( texte : str ) :
return texte == texte[::-1]
def palindrome2 ( texte : str ) :
debut = 0
fin = len (texte)-1
while debut < fin :
if texte[debut] != texte[fin]:
return False
else :
debut += 1
fin -= 1
return True
texte = input("Saisir un mot :")
if palindrome1(texte) :
print(f"{texte} est palindrome" )
else :
print(f"{texte} n'est pas palindrome" )
explique moi ligne par ligne
Assistant
User
Lerivez un programme qui récupère et affiche autant de caractères que possible, à partir d'une chaine queleonque, sous forme de suite pyramidale.
Aide / vous pouvez utiliser la fonetion len et les tranches de chaine (slicing).
Exemple !
Chaine - "abedefghiJkImnoparstuvwxyz"
a
bc
ghij
klmno
pqarstu
Remarque : les reste de la chaine "vwxyz" n'a pas été affiché, car sa taille est inferieur à la taille
de la ligne "parstu" incrementé de 1.
Assistant
Pour obtenir une sortie sous forme de suite pyramidale avec les contraintes données, nous pouvons écrire un programme Python suivant. Nous allons augmenter progressivement le nombre de caractères que nous extrayons de la chaîne à chaque étape, en commençant par 1 caractère, puis 2, et ainsi de suite, jusqu'à ce que nous ne puissions plus extraire un nombre suffisant de caractères pour former le niveau suivant de la pyramide.
python
Certainly! Let's break down the code and understand each line:
This function palindrome1 takes a string texte as an argument and checks if it is a palindrome. A palindrome is a word or phrase that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). The expression texte[::-1] creates a new string that is the reverse of texte. If texte is equal to its reverse, the function returns True (meaning it's a palindrome); otherwise, it returns False.
This is the start of the second function palindrome2, which also checks if a string is a palindrome. It initializes two indices: debut is set to 0 (the first character), and fin is set to the last character's index (len(texte) - 1).
python
while debut < fin:
This line starts a while loop that will continue as long as the debut index is less than the fin index.
python
if texte[debut] != texte[fin]:
return False
Inside the loop, if the characters at the positions debut and fin are not the same, the function immediately returns False, because this means that the string is not a palindrome.
python
else:
debut += 1
fin -= 1
If the characters at debut and fin are the same, the code enters the else block, where it moves the debut index forward by 1 (debut += 1) and the fin index backward by 1 (fin -= 1). This moves the indices closer to the center and prepares for the next comparison of characters.
python
return True
If the loop finishes without returning False, it means all the corresponding characters from the start and end are the same, so the function returns True. The word is a palindrome.
python
texte = input("Saisir un mot :")
This line prompts the user to input a word, which will be stored in the variable texte.
python
if palindrome1(texte):
print(f"{texte} est palindrome")
else:
print(f"{texte} n'est pas palindrome")
The if statement uses the function palindrome1 to check if texte is a palindrome. If it is, it prints out that the word is a palindrome; if it's not, it prints out that the word is not a palindrome.
Both functions palindrome1 and palindrome2 do the same thing but in different ways. palindrome1 does it by creating a reversed string and comparing it with the original, whereas palindrome2 checks character by character from both ends moving towards the center.
# Définir la chaîne d'entrée
chaine = "abedefghiJkImnoparstuvwxyz"
# Initialiser la taille de début de la pyramide
taille_pyramide = 1
# Initialiser l'index de début à 0
index = 0
while True:
# Calculer l'index de fin du morceau actuel de la chaîne
fin_morceau = index + taille_pyramide
# Vérifier si la longueur maximale n'est pas dépassée
if fin_morceau > len(chaine):
break
# Afficher le morceau de la chaîne qui forme le niveau actuel de la pyramide
print(chaine[index:fin_morceau])
# Mettre à jour l'index de début pour la prochaine itération
index = fin_morceau
# Augmenter la taille pour le niveau suivant de la pyramide
taille_pyramide += 1
Ce code récupère des caractères d'une chaîne fournie et les affiche de manière pyramidale. À chaque étape de la boucle while, il vérifie d'abord si l'extraction du nombre souhaité de caractères est possible sans dépasser la longueur de la chaîne. Si ce n'est pas possible, la boucle se termine. Autrement, il imprime la sous-chaîne actuelle, déterminée par les indices de début et de fin, puis ajuste ces indices pour la prochaine itération.
L'affichage que vous devriez obtenir avec la chaîne d'exemple chaine = "abedefghiJkImnoparstuvwxyz" sera :
code
a
be
def
ghiJ
kImno
parstu
Le reste de la chaîne "vwxyz" n'est pas affiché puisque sa taille (5) est inférieure à la taille de la ligne précédente (6) augmentée de 1.