165k views
4 votes
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

User Nekesha
by
6.3k points

1 Answer

5 votes

Answer:

Following are the program in the Python Programming Language.

#define function

def isPalindrome(test):

#set the if condition to check tuple is empty

if(len(test)==0):

return True

#Check the tuple contain 1 element

elif(len(test)==1):

return True

#check the element of tuple is palindrome or not

else:

lenth=len(test)

#check first last element is equal or not

if(test[0]==test[lenth-1] and isPalindrome(test[1:lenth-1] ) ):

#then, return true

return True

#otherwise

else:

#Return False,

return False

#define tuple type variable and initialize

test=(1,2,3,4,3,2,1)

#print and call the function

print(isPalindrome(test))

Output:

True

Step-by-step explanation:

Here, we define a function "palindrome()" and pass an argument in its parameter, inside the function.

  • Set the if conditional statement to check the following tuple is empty or not if the tuple is empty then, it returns true.
  • Set the elif conditional statement to check the following tuple containing one element, then it returns True.
  • Otherwise, we set the length of the tuple in the variable "lenth".
  • Then, set if conditional statement to check the first and the last element of the tuple is the same then, returns true.
  • Otherwise, it return false.
User Ilya Semenov
by
7.6k points