Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created October 26, 2012 07:26
Show Gist options
  • Save SegFaultAX/3957415 to your computer and use it in GitHub Desktop.
Save SegFaultAX/3957415 to your computer and use it in GitHub Desktop.
Greplin Challenge #1
import time
def longest_palindrome(s):
"""find the longest palindrome in string"""
palindrome = lambda s: s == s[::-1]
for n in range(len(s), 2, -1):
for i in range(0, len(s) - n + 1):
sub = s[i:i+n]
if palindrome(sub):
return sub
def timeit(fn, *args, **kwargs):
"""simple timing function"""
start = time.time()
res = fn(*args, **kwargs)
end = time.time()
print "result: %s in %.02fs" % (res, end - start)
return res
# s = open("gettysburg.txt").read()
import urllib2
s = urllib2.urlopen("http://challenge.greplin.com/static/gettysburg.txt").read()
timeit(longest_palindrome, s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment