J'essaye de suivre les instructions pour créer un palindrome. On me donne une demi-fonction et je dois remplir les blancs. Je ne parviens actuellement pas à faire fonctionner correctement la boucle. Je ne sais pas non plus comment ajouter des caractères au début ou à la fin de la chaîne sans utiliser le + ou une virgule. Je ne pense pas que ce soit ce qu'on me demande de faire. Voici les instructions;
La fonction is_palindrome vérifie si une chaîne est un palindrome ... Remplissez les espaces dans cette fonction pour retourner True si la chaîne passée est un palindrome, False sinon.
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = input_string.replace(" ", "") reverse_string = input_string.replace(" ", "") # Traverse through each letter of the input string for word in input_string: # Originally, I was only given the a FOR statement here, I wrote in the rest new_string+=word.replace(" ","").upper() # Add any non-blank letters to the # end of one string, and to the front # of the other string. if ___: new_string = ___ reverse_string = ___ # # Compare the strings if ___: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
J'ai supprimé les espaces vides et fait tout le même cas. J'ai assigné les caractères à new_string, mais il semble que je suis censé utiliser join pour ajouter les caractères, mais lorsque je fais, l'instruction print n'imprime rien. Je ne sais pas comment ajouter les éléments dans l'ordre inverse. Je ne sais même pas si je suis sur la bonne voie, car je ne suis pas sûr de ce que demande la déclaration IF. Je pense que je devrais être capable d'utiliser une boucle pour créer la chaîne, puis comparer les deux chaînes.
Quelqu'un pourrait-il également expliquer pourquoi new_string.join (word) n'affiche rien? Comment est-ce que je l'utilise incorrectement?
Merci beaucoup pour toute aide possible.
8 Réponses :
def is_palindrome(input_string): new_string = input_string.replace(" ", "").lower() reverse_string = input_string.replace(" ", "").lower()[::-1] if new_string == reverse_string: return True return False > This should do it.
J'ai écrit ce programme,
Il renvoie également le résultat attendu, mais il est rejeté.
Je ne sais pas exactement ce qu'ils testent en interne pour valider le code et son exactitude.
Ma mise en œuvre:
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = "" reverse_string = "" # Traverse through each letter of the input string for letter in input_string.strip(): # Add any non-blank letters to the # end of one string, and to the front # of the other string. if letter !=' ': new_string = new_string+letter reverse_string = letter+reverse_string # Compare the strings if new_string.lower() == reverse_string.lower(): return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
Si quelqu'un sait exactement ce que je manque ici?
Cela a fonctionné pour moi , j'ai essayé de changer un peu d'en haut mon code, cela fonctionne maintenant avec le code ci-dessous.
Vous pouvez le voir passer dans leur test et vérification du code, reportez-vous aux captures d'écran et au code ci-dessous.
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = "" reverse_string = "" # Traverse through each letter of the input string for letter in input_string.strip(): # Add any non-blank letters to the # end of one string, and to the front # of the other string. new_string = new_string+letter.replace(" ","") reverse_string = letter.replace(" ","")+reverse_string # Compare the strings if new_string.lower() == reverse_string.lower(): return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
Cela a fonctionné pour moi , ce code ce que le système s'attendait à voir.
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = "" reverse_string = "" # Traverse through each letter of the input string for word in input_string: # Add any non-blank letters to the # end of one string, and to the front # of the other string. word = word.lower() if word != " ": new_string = new_string + word reverse_string = word + reverse_string # Compare the strings if new_string == reverse_string: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = "" reverse_string = "" # Traverse through each letter of the input string for char in input_string: # Add any non-blank letters to the # end of one string, and to the front # of the other string. if char !=" ": new_string +=char.lower() reverse_string =char.lower()+reverse_string # Compare the strings if new_string==reverse_string: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
C'est assez simple. L'approche principale est
Insérer dans de nouvelles chaînes
True False True
Vérifiez si new-string == reverse_string
CODE:
# We'll create two strings, to compare them input_string = input_string.lower() new_string = "" reverse_string = "" # Traverse through each letter of the input string for i in input_string: # Add any non-blank letters to the # end of one string, and to the front # of the other string. if i !=" ": new_string = new_string+i reverse_string = i+reverse_string # Compare the strings if new_string == reverse_string: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
PRODUCTION
new_string = new_string+i reverse_string = i+reverse_string
Cela fonctionne pour moi:
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = input_string.replace(" ", "") new_string = new_string.lower() reverse_string = new_string[::-1] reverse_string = reverse_string.lower() # Traverse through each letter of the input string if new_string == reverse_string: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
En remarque, j'ai également essayé d'utiliser `` `` reverse_string = input_string [:: - 1] .replace ("", ""). Upper () `` `` Mais, je ne pense pas que ce soit le point. Je fais cela alors je peux simplement faire new_string == reverse_string. Je pense en tout cas. Merci.
def is_palindrome(input_string): # We'll create two strings, to compare them new_string = "" reverse_string = "" # Traverse through each letter of the input string for word in input_string: new_string+=word.replace(" ","").upper() reverse_string = new_string[::-1].replace(" ","").upper() if new_string == reverse_string: return True return False print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True
C'est ce que j'ai maintenant, mais supprimé en premier si l'instruction.