re.search solution in Clear category for The Longest Palindromic by Serg900vd
import re def longest_palindromic(a): result, index, match = a[0], 'a', True regex = '(?P[A-Za-z ])[A-Za-z ](?P=a)' while match: match = re.search(regex, a) if match: result = match[0] index += 'a' regex = f'(?P<{index}>[A-Za-z ]){regex}(?P={index})' return result 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!")
March 22, 2022
Comments: