Skip to content

Commit a5b776e

Browse files
authored
Create Sum to N.py
1 parent 1a19ddb commit a5b776e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

interview_query/Sum to N.py

+26
Original file line numberDiff line numberDiff line change
@@ -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+
return output
22+
23+
x = recursive([], integers, [])
24+
return x
25+
26+

0 commit comments

Comments
 (0)