Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using combinations and sorted() after len solution in Clear category for The Longest Palindromic by H0r4c3
from itertools import combinations
def longest_palindromic(a):
all_substrings = sorted([a[i:j] for i, j in combinations(range(len(a) + 1), r=2)], key=len, reverse=True)
for item in all_substrings:
if item == item[::-1]:
return item
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!")
Jan. 13, 2022
Comments: