Skip to content

Instantly share code, notes, and snippets.

@goish135
Created August 13, 2021 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goish135/b45e18b155fca58f6caeb13d414d2934 to your computer and use it in GitHub Desktop.
Save goish135/b45e18b155fca58f6caeb13d414d2934 to your computer and use it in GitHub Desktop.
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