I\'ve written a simple program in python which checks if the sentence is palindrome. But I can\'t figure out why isn\'t it working. The results is always False. Does anyone know
Since the mistake is already explained and the obvious s == s[::-1]
is already taken, I'll just throw a possibly minimal version of the original into the mix:
def isPalindrome(s):
s = s.strip().lower()
return not s or s[0] == s[-1] and isPalindrome(s[1:-1])
Note that you don't need replace(" ", "")
. Space on the outside is removed with strip()
now, and spaces on the inside will be removed by the strip()
later, in a call deeper into the recursion (if we don't stop earlier because a s[0] == s[-1]
fails).
You aren't returning the result of the function.
replace :
if word[0] == word[-1]:
isPalindrome(word[1:-1])
with
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
You're making this way more complicated than it has to be:
def palindrome(sentence):
sentence = sentence.strip().lower().replace(" ", "")
return sentence == sentence[::-1]
sentence[::-1]
uses string slicing to reverse the characters in the string.
A slightly more verbose solution that shows how the logic of the return
statement above works:
def palindrome(sentence):
sentence = sentence.strip().lower().replace(" ", "")
if sentence == sentence[::-1]:
return True
else:
return False
You need to replace "input" with "raw_input". Also, you are calling isPalindrome recursively and there is a mistake here as well. It should be:
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
else:
return False
Check the corrected code below:
def isPalindrome(word):
# Removes all spaces, and lowercase the word.
word = word.strip().lower()
word = word.replace(" ", "")
# If the length of the word is less than 1, means its a palindrome
if (len(word) <= 1):
return True
# Compares the first and the last character of the word.
# If it is the same, calls the function again with the same word, without its first and last characters.
# If its not the same, its not palindrome
if word[0] == word[-1]:
return isPalindrome(word[1:-1])
else:
return False
sentence = raw_input("Enter a sentence: \n")
if (isPalindrome(sentence)):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
I presume this is an assignment and the recursion is necessary, obviously return word == word[::-1]
is simpler but is not really relevant. You can write your recursive function a bit more succinctly:
def isPalindrome(word):
if not word:
return True
return word[0] == word[-1] and isPalindrome(word[1:-1])
word[0] == word[-1]
will either be True
or False
so you will either reach an empty string where not word
will True
so the recursion ends and the function returns True
or word[0] == word[-1]
will be False
so the function will return False
as and isPalindrome(word[1:-1])
will never be evaluated.
I would also maybe do the lowering outside of the function:
def isPalindrome(word):
if not word:
return True
return word[0] == word[-1] and isPalindrome(word[1:-1])
sentence = input("Enter a sentence: \n")
sentence = sentence.strip().lower()
sentence = sentence.replace(" ", "")
if isPalindrome(sentence):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
You algorithm is fine, the only problem is that you don't return the true result through the recursion, you have to return the isPalindrome result when calling it recursively:
else:
if word[0] == word[-1]:
return isPalindrome(word[1:-1]) #this changed
else:
return False