Advertisement
Woobinda

The Longest Palindromic

Jul 15th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. """
  2. Write a function that finds the longest palindromic substring of a given string. Try to be as efficient as possible!
  3. If you find more than one substring you should return the one which is closer to the beginning.
  4. """
  5.  
  6. from functools import reduce
  7.  
  8. def longest_palindromic(text):
  9.     result = []
  10.     while text:
  11.         _string = ''
  12.         for letter in text:
  13.             _string += letter
  14.             result.append(_string)
  15.         text = text[1:]
  16.     result = [x for x in result if x == reduce(lambda a,b: b+a, x)]
  17.     result = [x for x in result if len(x) == max(map(len, result))][0]
  18.     return result
  19.  
  20. if __name__ == '__main__':
  21.     assert longest_palindromic("artrartrt") == "rtrartr", "The Longest"
  22.     assert longest_palindromic("abacada") == "aba", "The First"
  23.     assert longest_palindromic("aaaa") == "aaaa", "The A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement