172k views
0 votes
Implement the function _isPalindrome() in palindrome.py, using recursion, such that it returns True if the argument s is a palindrome (ie, reads the same forwards and backwards), and False otherwise. You may assume that s is all lower case and doesn’t include any whitespace characters.

User Hardeep
by
6.0k points

1 Answer

6 votes

Answer:

The function is as follows:

def _isPalindrome(stri) :

if len(stri) == 0:

return True

if stri[0] == stri[len(stri) - 1]:

return _isPalindrome(stri[1:len(stri) - 1])

else:

return False

Step-by-step explanation:

This defines the function

def _isPalindrome(stri) :

If the string is empty, then it is palindrome

if len(stri) == 0:

return True

This checks if the characters at alternate positions are the same.

If yes, it calls the function to check for other characters

if stri[0] == stri[len(stri) - 1]:

return _isPalindrome(stri[1:len(stri) - 1])

If otherwise, the string is not palindrome

else:

return False

User Chasethesunnn
by
5.8k points