Skip to content

Instantly share code, notes, and snippets.

@seperman
Created October 29, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seperman/714b1d734831b3afa3f2ad0dd4d160fd to your computer and use it in GitHub Desktop.
Save seperman/714b1d734831b3afa3f2ad0dd4d160fd to your computer and use it in GitHub Desktop.
Pandolime
from copy import copy
text = "Hello maddam, how are you daad mamamaml?"
def find_palindromes(text):
i = 0
ir = 0
found = []
for i, v in enumerate(text):
if i >= 1:
if text[ir] == v:
ir -= 1
elif i - ir > 2:
found.append(text[ir:i])
else:
ir = i
def findLongestPalindrome(string):
return max([getPalindromeAt(i, string) for i in range(len(string))],
key=lambda a: len(a)) if len(string) > 0 else ''
def getPalindromeAt(position, string):
longest = (position, position)
for lower, upper in [(position - 1, position + 1),
(position, position + 1)]:
while lower >= 0 and upper < len(string) and string[lower] == string[upper]:
upper += 1
lower -= 1
longest = max(longest, (lower + 1, upper - 1),
key=lambda a: a[1] - a[0])
return string[longest[0]: longest[1] + 1]
print(find_palindromes(text))
print(findLongestPalindrome(text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment