python - Brute force closest pair algorithms - for loops -
new python. trying analyze algorithm closest pair of points. found example
which has these lines:
return min( ((abs(point[i] - point[j]), (point[i], point[j])) in range(numpoints-1) j in range(i+1,numpoints)), key=itemgetter(0))
it not clear me how loops being evaluated.
how loops related points, key , itemgetter?
when put code in ideone
i runtime error in times() function:
def times(): ''' time different functions ''' import timeit functions = [bruteforceclosestpair, closestpair] f in functions: print 'time for', f.__name__, timeit.timer( '%s(pointlist)' % f.__name__, 'from closestpair import %s, pointlist' % f.__name__).timeit(number=1)
thank you.
your min
code equivalent using generator follows:
def getpoints(point, numpoints): in range(numpoints - 1): j in range(i + 1, numpoints): yield (abs(point[i] - point[j]), (point[i], point[j])) return min(getpoints(point, numpoints), key=itemgetter(0))
as @dsm noted, first argument min
in code generator expression.
Comments
Post a Comment