Created
August 13, 2021 06:06
-
-
Save goish135/b45e18b155fca58f6caeb13d414d2934 to your computer and use it in GitHub Desktop.
This file contains 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(data, length): | |
def dfs(cur, pos): | |
if len(cur) == length: | |
yield cur | |
if pos == len(data): | |
return | |
if type(data) is list: | |
for i in range(pos, len(data)): | |
for j in dfs(cur + [data[i]], i + 1): | |
yield j | |
else: | |
for i in range(pos, len(data)): | |
for j in dfs(cur + data[i], i + 1): | |
yield j | |
if type(data) is list: | |
return dfs([], 0) | |
else: | |
return dfs("", 0) | |
''' | |
for i in combinations([1, 2, 3, 4], 2): | |
print(i) | |
for i in combinations(['a', 'b', 'c', 'd'], 2): | |
print(i) | |
for i in combinations('abcd', 2): | |
print(i) | |
''' | |
ans = [] | |
target = 7 | |
for j in range(1,len([2, 2, 2, 3, 3, 6, 7])): | |
for i in combinations([2, 2, 2, 3, 3, 6, 7], j): | |
#print(i) | |
if sum(i) == target: | |
#print(i) | |
if i not in ans: | |
ans.append(i) | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment