1 """Assorted useful combinatorics.
4 from __future__ import generators
7 def xcombinations(items, n):
8 """Iterate through ordered, length `n` subsets of `items`.
10 >>> for c in xcombinations([1, 2, 3], 2):
21 for i in xrange(len(items)):
22 for cc in xcombinations(items[:i]+items[i+1:],n-1):
25 def xunique_combinations(items, n):
26 """Iterate through unordered, length `n` subsets of `items`.
28 >>> for c in xunique_combinations([1, 2, 3], 2):
36 for i in xrange(len(items)):
37 for cc in xunique_combinations(items[i+1:],n-1):
40 def xselections(items, n):
41 """Iterate through all length `n` multisets of `items`.
43 >>> for c in xselections([1, 2, 3], 2):
57 for i in xrange(len(items)):
58 for ss in xselections(items, n-1):
61 def xpermutations(items):
62 """Iterate through all permutations of `items`.
64 >>> for c in xpermutations([1, 2, 3]):
73 return xcombinations(items, len(items))