codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
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)
Private
[
?
]
Run code
Submit