Skip to content

Instantly share code, notes, and snippets.

@czheo
Last active October 28, 2020 05:13
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 czheo/4046ec98c513109375f65cd9c677bbcd to your computer and use it in GitHub Desktop.
Save czheo/4046ec98c513109375f65cd9c677bbcd to your computer and use it in GitHub Desktop.
l = [2,3,5,7,9]
N = 13
def solve(l, N):
def helper(l, N, i, ans):
while i < len(l):
ans.append(l[i])
if N == l[i]:
result.append(ans[:])
elif N >= l[i] * 2:
helper(l, N - l[i], i, ans)
ans.pop()
i += 1
result = []
helper(sorted(l), N, 0, [])
return result
print(solve(l, N))
# [[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 5], [2, 2, 2, 7], [2, 2, 3, 3, 3], [2, 2, 9], [2, 3, 3, 5], [3, 3, 7], [3, 5, 5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment