[ create a new paste ] login | about

Link: http://codepad.org/l9Zzl6Gk    [ raw code | output | fork ]

aaronla - Python, pasted on May 8:
import time
c0 = time.clock()
def unyield (fn):
  def replacement (*args):
    c = []
    def Return (x):
      c.append(x)
      raise StopIteration()
    unyield_pump(lambda:fn(Return, *args))
    return c

  replacement.__name__ = fn.__name__ + "_unyield"
  return replacement

def unyield_pump (initiator, prefix=[]):
  it = initiator()
  xs = it.next()
  try:
    for p in prefix:
      xs = it.send(p)
    for x in xs:
      unyield_pump(initiator, prefix+[x])
  except StopIteration:
    pass

@unyield
def pythag(Return, amax, bmax,cmax):
  a = yield range(1,amax+1)
  b = yield range(a,bmax+1)
  c = yield range(b,cmax+1)
  if a*a+b*b==c*c:
    Return((a,b,c))

for triple in pythag(20,20,20):
  print triple
c1 = time.clock()
print "elapsed time: %s seconds"%(c1-c0)


Output:
1
2
3
4
5
6
7
(3, 4, 5)
(5, 12, 13)
(6, 8, 10)
(8, 15, 17)
(9, 12, 15)
(12, 16, 20)
elapsed time: 0.01 seconds


Create a new paste based on this one


Comments: