• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Untitled

a guest Mar 14th, 2013 28 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import collections
2. import itertools
3. import sys
4. import timeit
5.
6. def filtered_combi(n, r):
7.     def good(combo):
8.         return not any(combo[i]+1 == combo[i+1] for i in range(len(combo)-1))
9.     yield from filter(good, itertools.combinations(range(1, n+1), r))
10.
11. def non_consecutive_combinator(rnge, r, prev=[]):
12.     if r == 0:
13.         yield prev
14.
15.     else:
16.         for i, item in enumerate(rnge):
17.             for next_comb in non_consecutive_combinator(rnge[i+2:], r-1, prev+[item]):
18.                 yield next_comb
19.
20. def recursive_combi(n, r):
21.     yield from non_consecutive_combinator(range(1, n+1), r)
22.
23.
24. n0, n1, r0, r1 = map(int, sys.argv[1:])
25.
26. for n in range(n0, n1):
27.     for r in range(r0, r1):
28.         for f in filtered_combi, recursive_combi:
29.             print(n, r, f.__name__[:4], end=' ')
30.             x = len(list(f(n, r)))
31.             print(x, end=' ')
32.             if x:
33.                 t = timeit.timeit(lambda: collections.deque(f(n, r), maxlen=0), number=10000//n) / x
34.                 print(t, end=' ')
35.         print()
RAW Paste Data
Top