python 3.xx Recursion An \'array palindrome\' is an array, which, when its eleme
ID: 3823453 • Letter: P
Question
python 3.xx
Recursion
An 'array palindrome' is an array, which, when its elements are reversed, remains the same.
Write a recursive function, isPalindrome, that accepts a tuple and returns whether the tuple is a palindrome.
A tuple is a palindrome if:
*the tuple is empty or contains one element
*the first and last elements of the tuple are the same, and the rest of the tuple is a palindrome
More Hints:
You almost certainly should be using: False
You almost certainly should be using: True
You almost certainly should be using: [ ]
Explanation / Answer
The below definition will do the palindrome check and return true or false according to the parameter string that we call.
def isPalindrome(string):
if len(string) <= 1:
return True
if string[0] == ' ':
string = string[1:len(string)];
if string[len(string) - 1] == ' ':
string = string[0:len(string) - 1];
if string[0] != string[len(string) - 1]:
return False
return isPalindrome(string[1:len(string) - 1])
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.