0

I have written a program to find the longest palindrome in a given string. The problem is that I want to eliminate all instances of index variables to make the program more "pythonic". I have used enumerate to hold the index for each character in the input string, but I'm not sure how to eliminate the search index. Can anyone offer some insight. any tips on improving the program would be appreicated. thanks.

def isPALINDROME(input):
    lastINDEX=len(input)-1
    for i, each in enumerate(input):
        if input[i]==input[lastINDEX]:
            lastINDEX-=1
        else:
            return False
    return True

def findLONGEST_palindrome(input):
    list=[]
    strlen=len(input)-1
    for i, each in enumerate(input):
        j=i+1
        try:
            while input[i]!=input[j]:
                j+=1
            if input[i]==input[j]:
                list.append(input[i:j+1])
        except IndexError:
            pass
    return list

out=findLONGEST_palindrome("ASDFASDFDS12345678987654321ASDFjj")
for each in out:
    if isPALINDROME(each):
        print each
2
  • 4
    "pythonic" means "do the obvious straightforward thing that works", not "use every trick Python has in its sleeve". Commented Aug 30, 2013 at 15:05
  • 1
    What is your actual question? What do you mean by "eliminate the search index"? Commented Aug 30, 2013 at 15:10

1 Answer 1

2

What could be more pythonic here is to start to respect the standards and recommendations of the language, like PEP8 good practices:

  • Do not use built in functions/objects like input or list for variable names. This overrides the default behavior of those built-ins. s and l for straightforward objects could have been an option at least

  • Use coherent and simple case convention for your variable, object and function names. Prefer is_palindrome to isPALINDROME

  • Explicit is better than implicit: show the intention in your code whenever possible. Being a palindrome string means literally "the string is equal to the string in reverse order". A reverse string of sin Python will be s[::-1], so the function can be:

    def is_palindrome(s): return s==s[::-1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.