Last active
October 28, 2020 05:13
-
-
Save czheo/4046ec98c513109375f65cd9c677bbcd 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
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