Created
July 9, 2017 22:55
-
-
Save yixizhang/1ce682d01003ac16d67a3bf3377f03a7 to your computer and use it in GitHub Desktop.
comb
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 comb(n, k): | |
if k == n: | |
return [[1 for _ in range(n)]] | |
if k > n: | |
return False | |
if k == 0: | |
return [[0 for _ in range(n)]] | |
if k == 1: | |
ret = [] | |
for i in range(n): | |
l = [] | |
for j in range(n): | |
if i == j: | |
ele = 1 | |
else: | |
ele = 0 | |
l.append(ele) | |
ret.append(list(l)) | |
return ret | |
ret = [] | |
for i in range(n): | |
rest = comb(n - i - 1, k - 1) | |
if rest is False: | |
continue | |
l = [] | |
for j in range(i): | |
l.append(0) | |
l.append(1) | |
for r in rest: | |
ret.append(l + r) | |
return ret | |
print(comb(1, 0)) | |
print(comb(1, 1)) | |
print(comb(2, 1)) | |
print(comb(4, 1)) | |
print(comb(2, 3)) | |
print(comb(3, 2)) | |
print(comb(4, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment