-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsum_to_zero.py
More file actions
executable file
·49 lines (41 loc) · 988 Bytes
/
Copy pathsum_to_zero.py
File metadata and controls
executable file
·49 lines (41 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
"""
Objective: Write a function to find all the combinations of three numbers that sum to zero
Sample input:
[2, 3, 1, -2, -1, 0, 2, -3, 0]
Sample output:
2, -2, 0
1, -1, 0
3, -2, -1
3, 0, -3
3, 0, -3
"""
_input = [2, 3, 1, -2, -1, 0, 2, -3, 0]
_output = []
# divide into negative and positive
negative = []
positive = []
for i in _input:
if i<0:
negative.append(i)
else:
positive.append(i)
# make a unique list of numbers
negative = sorted(set(negative))
positive = sorted(set(positive))
for p in positive:
for n in negative:
if(p+n>0):
for i in negative:
if p+n+i > 0:
break
elif p+n+i == 0:
_output.append([p,n,i])
else:
for i in positive:
if p+n+i > 0:
break
elif p+n+i == 0:
_output.append([p,n,i])
for i in _output:
print i[0], i[1], i[2]