-1

I tried to write a recursive function that says if a string is a palindrome, but all I get is an infinite loop and I don't know what the problem is

def isPalindrome(S):
    listush=list(S) #listush=['a', 'b', 'n', 'n', 'b', 'a']
    length=len(listush) #length=6
    if length==0 or length==1:
        return S, "is a palindrome!"
    elif listush[0]!=listush[-1]:
        return S, "is not a palindrome!"
    else:
        del listush[0]
        del listush[-1]
        return isPalindrome(S)

print isPalindrome("abnnba")
dhke
  • 15,008
  • 2
  • 39
  • 56
Pythonist
  • 3
  • 1
  • 2
  • 1
    `del listush[0]` and `listush[-1]` don't delete characters from `S`, the list has nothing to do with `S` anymore. You pass on the original string into to the recursion without deleting the front and back chars. – dhke Jul 23 '16 at 13:55

5 Answers5

2

Hope this helps

def ispalindrome(word):

    if len(word)<=1:
        print("Palindrome")
        return
    else:
        if word[0]!=word[-1]:
            print("Not a palindrome")
            return
        return ispalindrome(word[1:len(word)-1])

word=input("Enter word ")

ispalindrome(word)
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
joc
  • 21
  • 2
1

First of all, indent your code properly.

Secondly, you are calling the function again with the same argument. Call with 'listush' list from which you are deleting or delete from 'S' and recurse with S argument.

Sachin
  • 3,576
  • 1
  • 15
  • 24
1

There's no need for creating a list. A python string is already an indexable sequence.

Even better, we can employ slicing and let the function return True and False instead of a tuple with text, With all of this, isPalindrome() becomes a one-liner:

def isPalindrome(S):
    return len(S) < 2 or (S[0] == S[-1] and isPalindrome(S[1:-2]))

print isPalindrome('A')
>>> True
print isPalindrome('AA')
>>> True
print isPalindrome('BAAB')
>>> True
print isPalindrome('ABAB')
>>> False
dhke
  • 15,008
  • 2
  • 39
  • 56
0

If you do an print(listush) you can see, that your list never changes. The following modification of your code works:

def isPalindrome(testStr, orig=None):
    if orig is None:
        orig = testStr
    length = len(testStr) #length=6
    print(testStr)
    if length == 0 or length == 1:
        return orig, "is a palindrome!"
    elif testStr[0] != testStr[-1]:
       return orig, "is not a palindrome!"
    else:
        return isPalindrome(testStr[1:-1], orig)

print isPalindrome("abnnba")
nidomiro
  • 820
  • 2
  • 10
  • 24
0

There are some things I would like to say about your code

  • You can send a slice of the list, saving you the trouble of deleting elements.
  • You don't need to convert it to a list, all the operations you need in finding palindrome are supported by strings.
  • You are returning S in the recursive function, which would be an empty list(or string) because it is diminishing each recursion. In recursive cases, I suggest you to just return True or False

Here is an example.

def isPalindrome(S):
    length=len(S)
    if length < 2:
        return True
    elif S[0] != S[-1]:
        return False
    else:
        return isPalindrome(S[1:length - 1])

Simple as that.

Rockybilly
  • 2,938
  • 1
  • 13
  • 38