Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Write a function that finds the longest palindromic substring of a given string. Try to be as efficient as possible!
- If you find more than one substring you should return the one which is closer to the beginning.
- """
- from functools import reduce
- def longest_palindromic(text):
- result = []
- while text:
- _string = ''
- for letter in text:
- _string += letter
- result.append(_string)
- text = text[1:]
- result = [x for x in result if x == reduce(lambda a,b: b+a, x)]
- result = [x for x in result if len(x) == max(map(len, result))][0]
- return result
- if __name__ == '__main__':
- assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
- assert longest_palindromic("abacada") == "aba", "The First"
- assert longest_palindromic("aaaa") == "aaaa", "The A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement