Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for The Longest Palindromic by Krischtopp
def longest_palindromic(text):
"""
We check all substrings from our text beginning with the full text:
"artrartrt" != "trtrartra"
"artrartr" != "rtrartra"
"rtrartrt" != "trtrartr"
"artrart" != "trartra"
"rtrartr" == "rtrartr"
"""
for length in reversed(range(1, len(text) + 1)):
for start in range(len(text) - length + 1):
subtext = text[start:start + length]
if subtext == subtext[::-1]:
return subtext
if __name__ == '__main__':
assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
assert longest_palindromic("abacada") == "aba", "The First"
assert longest_palindromic("aaaa") == "aaaa", "The A"
March 3, 2016