Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.