Parth
Parth

Reputation: 265

Longest palindrome in Python

I have a Python assignment which wants me write a program that finds the longest palindrome in a given text. I know there are examples of this function in other languages on this website, but I am a total beginner in Python and am having trouble writing the code.

This is how I am currently identifying palindromes:

def is_palindrome(word):
    x = 0 
    for i in range (len(word)/2):
        if (word[x]) == (word[len(word)-x-1]):
            x+=1
        if x == (len(word)/2):
            return True
    return False

Upvotes: 2

Views: 3134

Answers (3)

Soronbe
Soronbe

Reputation: 926

def fastLongestPalindromes(seq):
    seqLen = len(seq)
    l = []
    i = 0
    palLen = 0
    while i < seqLen:
        if i > palLen and seq[i - palLen - 1] == seq[i]:
            palLen += 2
            i += 1
            continue
        l.append(palLen)
        s = len(l) - 2
        e = s - palLen
        for j in range(s, e, -1):
            d = j - e - 1
            if l[j] == d: 
                palLen = d
                break
            l.append(min(d, l[j]))
        else:
            palLen = 1
            i += 1
    l.append(palLen)
    lLen = len(l)
    s = lLen - 2
    e = s - (2 * seqLen + 1 - lLen)
    for i in range(s, e, -1):
        d = i - e - 1
        l.append(min(d, l[i]))

    return l
def getPalindrome(text):
    lengths = fastLongestPalindromes(text)
    start = 0
    end = 0
    length = 0
    for i in range(len(lengths)):
        if(lengths[i] > length):
            length = lengths[i]
            end = i//2+(lengths[i]//2)
            start = i//2-(lengths[i]//2)
            if(i%2 == 1):
                start +=1
    return text[start:end]

In linear time. (longer code, but faster than the other answers, atleast for long strings).

Source: http://www.akalin.cx/longest-palindrome-linear-time (first function is copy pasted)

Upvotes: 1

kvivek
kvivek

Reputation: 3461

Alternate way

def Is_palindrome(word):
       return word==word[::-1]
# Assuming text is defined
print max((word for word in set(text.split()) if Is_Palindrome(word)), key=len)

Upvotes: 6

jkd
jkd

Reputation: 1045

I used:

def Is_palindrome(word):
    x = 0 
    for i in range (len(word)/2):
        if (word[x]) == (word[len(word)-x-1]):
            x+=1
        if x == (len(word)/2):
            return True
    return False

def longest_palindrome(text):
    lst = text.split() #Split it into words (cannot have punctuation)
    palindromes = [] #List that contains the palindromes
    long_len = 0 #Length of the longest palindrome
    longest = "" #The actual longest palindrome
    for i in lst: #Loop through all the words
        if Is_palindrome(i): #If the word is a palindrome
            palindromes.append(i) #Add it to the palindrome list
    for i in palindromes: #Loop through the palindrome list
        if len(i) > long_len: #If the palindrome is longer than the longest one
            longest = i #Set it as the longest one
            longest_len = len(i) # Set the length of the longest one to the length of this one
    return longest

Upvotes: 1

Related Questions