Created
October 29, 2016 23:02
-
-
Save seperman/714b1d734831b3afa3f2ad0dd4d160fd to your computer and use it in GitHub Desktop.
Pandolime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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