Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Longest Palindromic by masloo5000
def longest_palindromic(text):
result=''
for i in range(len(text)):
pal=text[i]
for m in range(1, len(text)): #przypadek typu -----H------
if i-m<0 or i+m>len(text)-1: break
if text[i-m]==text[i+m]:
pal+=text[i+m]
pal=text[i-m]+pal
else: break
if len(pal)>len(result):
result=pal
pal=''
m=0
while text[i-m]==text[i+m]: #przypadek typu -----HH-----
pal+=text[i+m]
pal=text[i-m]+pal
m+=1
if i-m<0 or i+1+m>len(text)-1: break
if len(pal)>len(result) and pal in text: #inaczej jest błąd że w "artrartrt" zwraca rtraartr mimo że powinno byc jedno 'a' mniej
result=pal
print(result)
return result
if __name__ == '__main__':
assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
assert longest_palindromic("abacada") == "aba", "The First"
assert longest_palindromic("aaaa") == "aaaa", "The A"
Nov. 18, 2018