Please help me to code correctly in Python.
T = int(input("Number of test cases: "))
for i in range(T 1):
def checkPalindrome(str):
l=len(str)
if l==0 or l==1:
return "It is a palindrome"
if str[0]!=str[-1]:
return "It is not a palindrome"
else:
str_new= str[1::-1]
output= checkPalindrome(str_new)
return output
str= [input(). split() for i in range(T) ]
checkPalindrome(str)
It is not working.
CodePudding user response:
The updated string for the next recursive call was not right. The format for slicing follows [start:end:step]
. So str[1::-1]
implied to start from index one and go back backward by 1 step (as denoted by -1).
The new string had to start from index one up to end excluding the last character. That can be done like this.
str_new = str[1:-1]
CodePudding user response:
For example:
def checkPalindrome(word):
l = len(word)
if l == 0 or l == 1:
return "It is a palindrome"
if word[0] != word[-1]:
return "It is not a palindrome"
return checkPalindrome(word[1:-1])
t = int(input("Number of test cases: "))
for i in range(t):
print(checkPalindrome(input()))