Skip to content

Instantly share code, notes, and snippets.

@swanson
Created October 8, 2010 20:17
Show Gist options
  • Select an option

  • Save swanson/617454 to your computer and use it in GitHub Desktop.

Select an option

Save swanson/617454 to your computer and use it in GitHub Desktop.
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
@swanson

swanson commented Oct 8, 2010

Copy link
Copy Markdown
Author

kinda hacky, but it worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment