0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • The duplicate question doesn't use recursion to check whether it's a palindrome, but it produces the correct output nonetheless. – Anderson Green Nov 26 '13 at 03:41

1 Answers1

0
def max_palindrome(s, start_at):
    # recursion base, a list with only 1 item is a palindrome OR 
    # a list that's equals to its reverse
    if len(s) == 1 or s == s[::-1]:
        return (len(s), start_at)
    # if we got here the current list is not a palindrome, 
    # lets try to scan it by taking out one element from each of its sides
    return max(max_palindrome(s[1:], start_at+1), 
       max_palindrome(s[:-1], start_at))

for p in candidatePs :
    print p, "max", max_palindrome(p)

output:

[1] max (1, 0)
[0, 1, 2, 3, 4, 5, 6, 7] max (1, 7)
[0, 1, 2, 3, 3, 2, 1, 0] max (8, 0)
[0, 1, 2, 3, 0, 3, 2, 1, 0] max (9, 0)
[0, 1, 2, 0, 1, 2, 3, 0, 3, 2, 1, 0] max (9, 3)
[8, 3, 2, 3] max (3, 1)
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42
  • I'm confused on how this helps me with my question on to why for [8,3,2,3] i get max (0,0) and not max(3,1). I have to use def maxpalindrome(thelist) for my code I so I do not see how what you wrote helps me with my question. Maybe a bit more explanation would help me. –  Nov 26 '13 at 04:56
  • updated to use your example, let me know if it helps – Guy Gavriely Nov 26 '13 at 05:04
  • Im still a little confused, since in my code the palindrome [8,3,2,3] is part of the candidatePs so I need to have the code at part of my def maxpalindrome(thelist) how would I take what you wrote and add it to that section. without explicitly stating the palindrome = [8,3,2,3] and print maxpalindrome –  Nov 26 '13 at 05:12
  • added explanations in the code, the code I wrote is the entire answer and was not supposed to be integrate with other (your) code – Guy Gavriely Nov 26 '13 at 05:17
  • updated to use entire candidatePs set – Guy Gavriely Nov 26 '13 at 05:25
  • Ok, so in my code I need a way to scan it, by taking off en element from each side. Is that the return max(max_palindrome(s[1:], start_at+1) part of it? Because I don't even know how to even start something like that in my code, to be able to adapt what you explained into my own. –  Nov 26 '13 at 05:27
  • I thought you'd leave your code and take this as it is, after you fully understand what it does ofc... – Guy Gavriely Nov 26 '13 at 05:30
  • This is a practice problem I am working on and the def maxPalindrome( thelist ) : and the #test down where given to work from, so I am trying to work my code around that –  Nov 26 '13 at 05:43