Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
classic solution in Clear category for The Longest Palindromic by kdim
def longest_palindromic(a):
p = [a[i:j] for i in range(len(a)) for j in range(i+1, len(a)+1) if a[i:j] == a[i:j][::-1]]
return sorted(p, key=lambda x: (-len(x), p.index(x)))[0]
if __name__ == '__main__':
print("Example:")
print(longest_palindromic('abc'))
# These "asserts" are used for self-checking and not for an auto-testing
assert longest_palindromic('abc') == 'a'
assert longest_palindromic('abacada') == 'aba'
assert longest_palindromic('artrartrt') == 'rtrartr'
assert longest_palindromic('aaaaa') == 'aaaaa'
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 1, 2021
Comments: