简体   繁体   English

Python中最长的回文

[英]Longest palindrome in Python

I have a Python assignment which wants me write a program that finds the longest palindrome in a given text. 我有一个Python作业,要我编写一个程序,在给定的文本中找到最长的回文。 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. 我知道该网站上有其他语言的此功能示例,但我是Python的入门者,编写代码时遇到了麻烦。

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

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)

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
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) 来源: http//www.akalin.cx/longest-palindrome-linear-time (第一个功能是复制粘贴)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM