Created
November 5, 2013 09:04
-
-
Save farawayboat/7315964 to your computer and use it in GitHub Desktop.
使用递归生成组合数.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def combinations(iterable, r): | |
pool = tuple(iterable) | |
n = len(pool) | |
if r > n: | |
return | |
def comb_core(n, r): | |
for i in reversed(range(r - 1, n)): | |
if (r == 1): | |
yield (i, ) | |
else: | |
for out in comb_core(i, r - 1): | |
yield (i, ) + out | |
for out in comb_core(n, r): | |
yield tuple(pool[i] for i in out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment