Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Longest Palindromic by marcelina.gorzelana
def longest_palindromic(text):
res = ""
i = 1
pal = 0
while i < len(text):
for j in range(i):
first = j
last = i
while text[first] == text[last] and first <= j + ((i - j) / 2):
first+= 1
last-= 1
first-= 1
last+= 1
if first == last or last == first + 1:
if i - j > pal:
pal = i - j
res = text[j:i+1]
i+= 1
if res == "":
res = text[0]
return res
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. 25, 2016