To count number of quadruplets with sum = 0

n00m n00m at narod.ru
Fri Mar 16 01:49:03 EDT 2007


Steven, I ran this:

import time, collections, itertools
t = time.clock()

q,w,e,r,sch = [],[],[],[],0

h = collections.defaultdict(itertools.repeat(0).next)

f = open("D:/m4000.txt","rt")

for o in range(int(f.readline())):
  row = map(int, f.readline().split())
  q.append(row[0])
  w.append(row[1])
  e.append(row[2])
  r.append(row[3])

f.close()

for x in q:
  for y in w:
    h[x+y] += 1

for x in e:
  for y in r:
    sch += h[-(x + y)]

q,w,e,r,h = None,None,None,None,None

print sch
print time.clock() - t


========= and it almost froze my PC...
=== but it was faster than my code on input file with 1000 rows:
====== 2.00864607094s VS 3.14631077413s




More information about the Python-list mailing list