Anonymous user
Hofstadter Figure-Figure sequences: Difference between revisions
Hofstadter Figure-Figure sequences (view source)
Revision as of 09:07, 25 August 2012
, 11 years ago→Using cyclic iterators: replace with simpler and faster code: implicit stuff seem to work better
(→Using cyclic iterators: replace with simpler and faster code: implicit stuff seem to work better) |
|||
Line 1,085:
===Using cyclic iterators===
{{trans|Haskell}}
<lang python>from itertools import islice
def
n = 1
yield n
n += s
yield n;
u = 5
if r <= u: continue;
for x in range(u, r): yield x
u = r + 1
def lst(s, n): return list(islice(s(), n))
▲ def R_gen(S):
▲ for s in S:
▲ yield 2
▲ yield 4
▲ for s in count(5):
print "R:", lst(R, 10)
print sorted(lst(R, 40) + lst(S, 960)) == list(range(1,1001))
# perf test case
# print sum(
▲print(list(islice(S, 10)))
{{out}}
S: [
▲[2, 4, 5, 6, 8, 9, 10, 11, 13, 14]
True
</pre>
|