To count number of quadruplets with sum = 0

n00m n00m at narod.ru
Fri Mar 16 01:57:13 EDT 2007


Paul,

import time
t = time.clock()
f = open("D:/m4000.txt","rt")
npairs = int(f.readline())
quads = [map(int, f.readline().split()) for i in xrange(npairs)]
f.close()
da = {}
for p in quads:
    for q in quads:
        z = p[2] + q[3]
        da[z] = da.get(z,0) + 1
print sum(da.get(-(p[0]+q[1]), 0) for p in quads for q in quads)
print time.clock() - t


Two first outputs is of above (your) code; next two - of my code:

>>> ================================ RESTART ===
>>>
0
68.9562762865
>>> ================================ RESTART ===
>>>
0
68.0813539151
>>> ================================ RESTART ===
>>>
0
62.5012896891
>>> ================================ RESTART ===
>>>
0
62.5030784639




More information about the Python-list mailing list