To count number of quadruplets with sum = 0

n00m n00m at narod.ru
Sat Mar 17 14:05:23 EDT 2007


i have no NumPy to test it...
without Psyco Anton's code is the winner: ~48sec vs ~58sec of my code
But with Psyco my runtime is ~28sec; Anton's - ~30sec (PC: 1.6 ghz,
512 mb)
Not so bad.. keeping in mind that 256000 billions quadruplets to
check :)


import psyco, time
psyco.full()
t = time.clock()

def main():
  q,w,e,r,sch,h = [],[],[],[],0,{}

  f = open("D:/m4000.txt","rt")
  for o in range(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:
      if h.has_key(x+y):
        h[x+y] += 1
      else:
        h[x+y] = 1

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

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

main()
print time.clock() - t




More information about the Python-list mailing list