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 Bartosz_M
def longest_palindromic(text):
possible = []
palindroms_dict = {}
if text == text[::-1]:
return text
for i in range(len(text) - 1):
for j in range(len(text)):
possible.append(text[i - 1:j + 1])
for word in possible:
if word == word[::-1] and word != "":
if len(word) not in palindroms_dict:
palindroms_dict[len(word)] = [word]
else:
palindroms_dict[len(word)].append(word)
if len(palindroms_dict) == 1:
return text[0]
return(palindroms_dict[max(palindroms_dict)][0])
if __name__ == '__main__':
assert longest_palindromic("rtratr") == "rtrartr", "The Longest"
assert longest_palindromic("abacada") == "aba", "The First"
assert longest_palindromic("aaaa") == "aaaa", "The A"
Nov. 13, 2016