Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Babbage's Difference Engine in Python
def diff(seq):
""" [1,4,9] => [3,5] """
return map(lambda x,y: x-y, seq[1:], seq[:-1])
def triangle(seq):
""" [1,4,9] => [[1,4,9],[3,5],[2]] """
for i in range(1, len(seq)):
return diffs
def reductions(seq):
""" [1,4,9] => [1,5,14] """
l = [seq[0]]
for i in range(1, len(seq)):
return l
def infer(pattern, n):
""" infer([1,4,9], 10) => [1,4,9,16,25 ... 100] """
gen_seq = [seq[-1] for seq in triangle(pattern)]
elements = pattern[:-1]
for count in range(len(pattern)-1, n):
gen_seq = reductions(gen_seq)
return elements

This comment has been minimized.

Copy link
Owner Author

@divs1210 divs1210 commented Oct 14, 2014

This is a Python port of some Clojure code that I wrote about some time back in a blog post .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.