I'm working on a Python code, where I have to test whether a list is a palindrome using recursion and am coming across come confusion and issues with my code:
def isPalindrome( thesublist ) :
thesublisttest = thesublist[0:]
if len(thesublisttest) <= 1:
return True
elif len(thesublisttest) == 2:
x = thesublisttest[0]
y = thesublisttest[1]
if x == y:
return True
else:
return false == thesublisttest.pop(0)
elif len(thesublisttest) > 2:
first = thesublisttest.pop(0)
last = thesublisttest.pop()
if first == last:
return isPalindrome(thesublisttest)
else:
return False
def maxPalindrome( thelist ) :
completelist=thelist[:]
completelist.reverse()
complete=len(thelist)-1
for i in range(complete):
if completelist[:]==thelist[:]:
x=len(thelist)
y=0
return(x,y)
elif completelist[i:complete]==thelist[i:complete]:
successlist=thelist[i:complete]
a=i
b=len(thelist)-a
return (a,b)
thelisttest = thelist[0:]
if thelisttest:
return (0,0)
# test
candidatePs = [
[1,],
range(8),
range(4)+range(3,-1,-1),
range(4)+[0]+range(3,-1,-1),
range(3)+range(4)+[0]+range(3,-1,-1),
[8,3,2,3],
]
for p in candidatePs :
print p, isPalindrome( p )
print p, "max", maxPalindrome( p )
I am unsure if what I am doing is considered recursion and I also know the [8,3,2,3] should show max(3,1) and my code spits it out as max (0,0) Any assistance in my code would be of great help.