For example:
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
import difflib | |
def longest_palindrome(text): | |
matcher = difflib.SequenceMatcher(None,text,text[::-1]) | |
length = len(text) | |
a,b,k = matcher.find_longest_match(0,length-1,0,length-1) | |
return text[a:a+k] |
will find the longest palindrome in a string, e.g.
>>> from palindrome import * >>> longest_palindrome('asdfracecarasdf') 'racecar' >>> longest_palindrome('asdffasdfaipreferpiasdfasdfa') 'aipreferpia'
No comments:
Post a Comment