50b7bab397d36e98722a7fd8e80f42a5701faacd
[poker.git] / combinations.py
1 """Assorted useful combinatorics.
2 """
3
4 from __future__ import generators
5
6
7 def xunique_combinations(items, n):
8     """Iterate through unordered, length `n` subsets of `items`.
9
10     >>> for c in xunique_combinations([1, 2, 3], 2):
11     ...     print c
12     [1, 2]
13     [1, 3]
14     [2, 3]
15     """
16     if n==0: yield []
17     else:
18         for i in xrange(len(items)):
19             for cc in xunique_combinations(items[i+1:],n-1):
20                 yield [items[i]]+cc