Created
October 8, 2010 20:17
-
-
Save swanson/617454 to your computer and use it in GitHub Desktop.
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
data = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth' | |
def longest_pal(l, r, subs): | |
if l - 1 >= 0 and r + 1 < len(data): | |
if is_pal(data[l-1] + subs + data[r+1]): | |
return longest_pal(l - 1, r + 1, data[l-1] + subs + data[r+1]) | |
else: | |
return subs | |
else: | |
return subs | |
def is_pal(s): | |
return s == s[::-1] | |
m = 0 | |
word = '' | |
for i in range(len(data)): | |
x = longest_pal(i, i, data[i]) | |
if len(x) > m: | |
m = len(x) | |
word = x | |
print m, word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kinda hacky, but it worked