Using Python write a recursive function that takes text as a parameter and then
ID: 662500 • Letter: U
Question
Using Python write a recursive function that takes text as a parameter and then judges whether or not the text represents a palindrome. To make the judging a bit more flexible, you should not differentiate between upper and lowercase letters. An example is the name Bob. Bob will be judged to be a palindrome. Name the function isPalidrome. Have it return a Boolean based on whether or not the text is a palindrome. Remember, your code must be recursive. Do not use a for loop, a while loop, nor any reversing trick.
Explanation / Answer
def isPalindrome(s):
if s == '':
return True
else:
if (ord(s[0]) - ord(s[len(s)-1])) == 0:
return
isPalindrome(s[1:len(s)-1])
//recursive function
else:
return False
print isPalindrome('bob') -->true
print isPalindrome('abab') -->false
print isPalindrome('abba') -->true
print isPalindrome('andrea') -->false
print isPalindrome('abaaba') --->true
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.