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

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -