Created
October 26, 2012 07:26
-
-
Save SegFaultAX/3957415 to your computer and use it in GitHub Desktop.
Greplin Challenge #1
This file contains hidden or 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 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