Question
A palindrome is a sequence of characters which reads the same backward or forward. For example, anna is a palindrome because reversing the letters gives
A palindrome is a sequence of characters which reads the same backward or forward. For example, "anna" is a palindrome because reversing the letters gives "anna", which is the same as when the letters are not reversed.
Write a recursive function called is_palindrome that takes a string named word as its parameter and returns True if word has length less than or equal to 1. If the length of word is greater than 1, the function should return False if the first character of word is different to the last character of word. If the length of word is greater than 1, and the first and last characters of the string are identical, the function should return the result of is_palindrome() with the parameter of word with its first and last characters removed (e.g. is_palindrome("anna") should return the result of is_palindrome("nn")).
def is_palindrome(word): pass # TODO: Implement this function
possible_palindrome = input("Enter a word/phrase to check: ") if is_palindrome(possible_palindrome): print(possible_palindrome, "is a palindrome") else: print(possible_palindrome, "is not a palindrome")
TESTS:
a
Expected Output
Enter a word/phrase to check: a a is a palindrome
anna
Expected Output
Enter a word/phrase to check: anna anna is a palindrome
banana
Expected Output
Enter a word/phrase to check: banana banana is not a palindrome
racecar
Expected Output
Enter a word/phrase to check: racecar racecar is a palindrome
abca
Expected Output
Enter a word/phrase to check: abca abca is not a palindrome
Anna
Expected Output
Enter a word/phrase to check: Anna Anna is not a palindrome
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started