皆さん、私はPython 2.7で最も長い回文アルゴリズムの課題に苦労しています。私は近づいていますが、私は理解できない小さな誤りがあります。私が働いて回文を持っていますが、正しい印刷する最長の回文を得ることができない、どちらか私が理解から、私の文字バッファエラーや印刷物0Pythonの最長回文アルゴリズムに固執2.7
def palindrome(string):
string = "".join(str.split(" ")).lower()
i = 0
while i < len(string):
if string[i] != string[(len(string) - 1) - i]:
return False
i += 1
return True
print palindrome("never odd or even")
def longest_palindrome(string):
best_palindrome = 0
i1 = 0
while i1 < len(string):
length = 1
while (i1 + length) <= len(string):
substring = string.split(i1,length)
if palindrome(substring) and (best_palindrome == 0 or len(substring) > len(best_palindrome)):
best_palindrome = substring
length += 1
i1 += 1
return best_palindrome
print longest_palindrome("abcbd")
あなたがしていることを理解できるように、「最長の回文アルゴリズムの課題」について説明してください。 –
あなたの回文機能は何かナンセンスをしています。 –
@OPが[Manacher's Algorithm](https://en.wikipedia.org/wiki/Longest_palindromic_substring)を実装しようとしていると思います。 – shash678