We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1a19ddb commit a5b776eCopy full SHA for a5b776e
interview_query/Sum to N.py
@@ -0,0 +1,26 @@
1
+Given a list of integers, and an integer N,
2
+write a function sum_to_n to find all combinations that sum to the value N.
3
+
4
5
6
+ def sum_to_n(integers , target):
7
8
9
+ def recursive(curlist, candidates, output):
10
11
+ if sum(curlist) == target:
12
+ output.append(curlist)
13
+ return output
14
15
+ else:
16
+ for i in range(len(candidates)):
17
+ if sum(curlist) + candidates[i] <=target:
18
+ recursive( curlist + [candidates[i]] ,candidates[i:],output )
19
20
21
22
23
+ x = recursive([], integers, [])
24
+ return x
25
26
0 commit comments