-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.py
63 lines (57 loc) · 1.14 KB
/
3sum.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def threeSum(S,dic,key):
mydict={}
for i in range(0,len(S)):
if not S[i] in mydict:
mydict[S[i]]=i
#print mydict
S.sort()
n=len(S)
for i in range(0,n-2):
a = S[i];
start = i+1;
end = n-1;
while (start < end):
b = S[start]
c = S[end]
if (a+b+c == 0 and (a<b) and(b<c)):
#print "A=",a,"B=",b,"C=",c
temp = [mydict[a]+1,mydict[b]+1,mydict[c]+1]
temp.sort()
dic[key].append(temp[0])
dic[key].append(temp[1])
dic[key].append(temp[2])
start = start + 1
end = end - 1
break
elif (a+b+c > 0):
end = end - 1
else:
start = start + 1
if dic[key]:
break
if dic[key]==[]:
dic[key].append(-1)
f = open("rosalind_3sum.txt",'r')
num=[]
num = f.readline()
srtd =num.strip()
srtd = map(int, srtd.split(' '))
line = srtd[0]
#print line
size = srtd[1]
dic={}
for i in range(1,line+1):
dic[i]=[]
for i in range(1,line+1):
num = f.readline()
A = num.strip()
A = map(int, A.split(' '))
threeSum(A,dic,i)
f.close()
ff = open("output14.txt",'w')
for i in range(1,line+1):
arr=dic[i]
for j in range(0,len(arr)):
ff.write(str(arr[j])+" ")
ff.write("\n")
ff.close()