Closest Pair Implemetation Python
问题 I am trying to implement the closest pair problem in Python using divide and conquer, everything seems to work fine except that in some input cases, there is a wrong answer. My code is as follows: def closestSplitPair(Px,Py,d): X = Px[len(Px)-1][0] Sy = [item for item in Py if item[0]>=X-d and item[0]<=X+d] best,p3,q3 = d,None,None for i in xrange(0,len(Sy)-2): for j in xrange(1,min(7,len(Sy)-1-i)): if dist(Sy[i],Sy[i+j]) < best: best = (Sy[i],Sy[i+j]) p3,q3 = Sy[i],Sy[i+j] return (p3,q3,best