Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.43 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def get_longest_palindrome(text):
  2.     best_palindrome = ''
  3.     length = len(text)
  4.     for i in range(length * 2 - 1):
  5.         start = i / 2 - i % 2
  6.         end = i / 2 + 1
  7.         while start > 0 and end < length and text[start] == text[end]:
  8.             start -= 1
  9.             end += 1
  10.         candidate = text[start+1:end]
  11.         if len(candidate) > len(best_palindrome):
  12.             best_palindrome = candidate
  13.     return best_palindrome
clone this paste RAW Paste Data