Skip to content

Instantly share code, notes, and snippets.

@mquezada
Created June 21, 2012 05:45
Show Gist options
  • Save mquezada/2964088 to your computer and use it in GitHub Desktop.
Save mquezada/2964088 to your computer and use it in GitHub Desktop.
import sys
def hamming(p1,p2,p3):
yield 1
for n in imerge( imult(p1, hamming(p1,p2,p3)), imerge( imult(p2, hamming(p1,p2,p3)), imult(p3, hamming(p1,p2,p3)) ) ):
yield n
def imult(a, xs):
x = xs.next()
while True:
yield a*x
x = xs.next()
def imerge(xs, ys):
x = xs.next()
y = ys.next()
while True:
if x == y:
yield x
x = xs.next()
y = ys.next()
elif x < y:
yield x
x = xs.next()
else:
yield y
y = ys.next()
def hamming_i(p1,p2,p3,i):
g = hamming(p1,p2,p3)
for j in range(i+1):
a = g.next()
return a
def main():
if len(sys.argv) != 5:
print "hamming.py p1 p2 p3 i"
raise SystemExit
p1 = int(sys.argv[1])
p2 = int(sys.argv[2])
p3 = int(sys.argv[3])
i = int(sys.argv[4])
print hamming_i(p1,p2,p3,i)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment