Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Created October 8, 2010 22:38
Show Gist options
  • Save jiaaro/617686 to your computer and use it in GitHub Desktop.
Save jiaaro/617686 to your computer and use it in GitHub Desktop.
def part1():
s = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth'
substrs = (s[start:l+start]
for l in range(1, len(s))
for start in range(0, len(s)-l)
)
substrs = ((len(ss), ss) for ss in substrs if s.find(ss[::-1]) != -1)
return max(substrs, key=lambda x: x[0])
# part 2
def is_prime(n):
for x in range(2, int(n**0.5)+1):
if n%x == 0:
return False
return True
def fib_gen():
x = y = 1
while True:
z = x + y
yield z
x, y = y, z
def get_prime_fib_gt(n):
fibs = fib_gen()
while True:
fib = fibs.next()
if fib < n:
continue
if is_prime(fib):
return fib
def part2():
X = get_prime_fib_gt(227000)
prime_divisors = (n for n in range(2, X+1)
if (X+1)%n == 0
and is_prime(n))
return sum(prime_divisors)
# part 3
def part3():
from itertools import combinations
nums = [3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99]
combs = (combinations(nums, l)
for l in range(2, len(nums))
)
return len([c for cs in combs
for c in cs
if sum(c[:-1]) == c[-1]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment