Ordering cells by their distance from center cell(s)
I had someone ask me, what at that time seemed an innocent enough question:

How do we ordering cells in a 2D array by their distance from predefined/precomputed center cell(s).

Here is a table showing how far the particular cell is from predefined center cells ( they have values of 0 in them ). A value of n means it's n cells away from the center:


    +----+----+----+----+----+----+----+----+
    | 4  | 4  | 3  | 3  | 3  | 3  | 4  | 4  |
    +----+----+----+----+----+----+----+----+
    | 4  | 3  | 2  | 2  | 2  | 2  | 3  | 4  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 1  | 1  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 0  | 0  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 0  | 0  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 1  | 1  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 4  | 3  | 2  | 2  | 2  | 2  | 3  | 4  |
    +----+----+----+----+----+----+----+----+
    | 4  | 4  | 3  | 3  | 3  | 3  | 4  | 4  |
    +----+----+----+----+----+----+----+----+

I solved the problem by computing the straight line distance between ( x1, y1 ) and ( x2, y2 ) in an Euclidean space and sorting them using the old school "Decorate-Sort-Undecorate" method.

This is what I ended up with:

    import math
    
    boardMaxRow = 8
    
    boardMaxCol = 8
    
    thatAbsurdLargeValue = ( 1 + boardMaxRow + boardMaxCol )
    
    centerCells = ( ( 3, 3 ), ( 3, 4 ), ( 4, 3 ), ( 4, 4 ) )
    
    cellsOrderedFromTheCenter = {}
    
    for row in xrange( boardMaxRow ):
    
        for col in xrange( boardMaxCol ):
    
            minDistanceFromCenter = thatAbsurdLargeValue
    
            for ( centerX, centerY ) in centerCells:
    
                # straight line distance between ( x1, y1 ) and ( x2, y2 ) in an Euclidean space
    
                distanceFromCenter = int( 0.5 + math.sqrt( ( row - centerX ) ** 2 + ( col - centerY ) ** 2 ) )
    
                minDistanceFromCenter = min( minDistanceFromCenter, distanceFromCenter )
    
            cellsOrderedFromTheCenter[ ( row, col ) ] = minDistanceFromCenter
    
    board = [ keyValue for keyValue in cellsOrderedFromTheCenter.items() ]
    
    import operator
    
    # sort the board in ascending order of distance from the center
     
    board.sort( key = operator.itemgetter( 1 ) )
    
    boardWithCellsOrderedFromTheCenter = [ key for ( key , Value ) in board ]
    
    print boardWithCellsOrderedFromTheCenter

The output:

    [(3, 3), (4, 4), (4, 3), (3, 4), (5, 4), (2, 5), (2, 2), (5, 3), (3, 2), (4, 5), (5, 5), (2, 3), (4, 2), (3, 5), (5, 2), (2, 4), (1, 3), (6, 4), (5, 6), (2, 6), (5, 1), (1, 2), (6, 3), (1, 5), (3, 6), (4, 1), (1, 4), (2, 1), (6, 5), (4, 6), (3, 1), (6, 2), (7, 3), (4, 7), (3, 0), (1, 6), (3, 7), (0, 3), (7, 2), (4, 0), (2, 0), (5, 7), (1, 1), (2, 7), (6, 6), (5, 0), (0, 4), (7, 5), (6, 1), (0, 2), (7, 4), (0, 5), (0, 7), (6, 7), (7, 6), (7, 7), (0, 0), (7, 1), (6, 0), (1, 0), (0, 1), (7, 0), (0, 6), (1, 7)]

I am amazed at how much code I got in there, for such a trivial problem.

My question is : can I make it faster and/or shorter ( use less temporaries/function calls )?
Ordering cells by their distance from center cell(s)
===

I had someone ask me, what at that time seemed an innocent enough question:

How do we ordering cells in a 2D array by their distance from predefined/precomputed center cell(s).

Here is a table showing how far the particular cell is from predefined center cells ( they have values of 0 in them ). A value of n means it's n cells away from the center:


    +----+----+----+----+----+----+----+----+
    | 4  | 4  | 3  | 3  | 3  | 3  | 4  | 4  |
    +----+----+----+----+----+----+----+----+
    | 4  | 3  | 2  | 2  | 2  | 2  | 3  | 4  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 1  | 1  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 0  | 0  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 0  | 0  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 3  | 2  | 1  | 1  | 1  | 1  | 2  | 3  |
    +----+----+----+----+----+----+----+----+
    | 4  | 3  | 2  | 2  | 2  | 2  | 3  | 4  |
    +----+----+----+----+----+----+----+----+
    | 4  | 4  | 3  | 3  | 3  | 3  | 4  | 4  |
    +----+----+----+----+----+----+----+----+

I solved the problem by computing the straight line distance between ( x1, y1 ) and ( x2, y2 ) in an Euclidean space and sorting them using the old school "Decorate-Sort-Undecorate" method.

This is what I ended up with:

    import math
    
    boardMaxRow = 8
    
    boardMaxCol = 8
    
    thatAbsurdLargeValue = ( 1 + boardMaxRow + boardMaxCol )
    
    centerCells = ( ( 3, 3 ), ( 3, 4 ), ( 4, 3 ), ( 4, 4 ) )
    
    cellsOrderedFromTheCenter = {}
    
    for row in xrange( boardMaxRow ):
    
        for col in xrange( boardMaxCol ):
    
            minDistanceFromCenter = thatAbsurdLargeValue
    
            for ( centerX, centerY ) in centerCells:
    
                # straight line distance between ( x1, y1 ) and ( x2, y2 ) in an Euclidean space
    
                distanceFromCenter = int( 0.5 + math.sqrt( ( row - centerX ) ** 2 + ( col - centerY ) ** 2 ) )
    
                minDistanceFromCenter = min( minDistanceFromCenter, distanceFromCenter )
    
            cellsOrderedFromTheCenter[ ( row, col ) ] = minDistanceFromCenter
    
    board = [ keyValue for keyValue in cellsOrderedFromTheCenter.items() ]
    
    import operator
    
    # sort the board in ascending order of distance from the center
     
    board.sort( key = operator.itemgetter( 1 ) )
    
    boardWithCellsOrderedFromTheCenter = [ key for ( key , Value ) in board ]
    
    print boardWithCellsOrderedFromTheCenter

The output:

    [(3, 3), (4, 4), (4, 3), (3, 4), (5, 4), (2, 5), (2, 2), (5, 3), (3, 2), (4, 5), (5, 5), (2, 3), (4, 2), (3, 5), (5, 2), (2, 4), (1, 3), (6, 4), (5, 6), (2, 6), (5, 1), (1, 2), (6, 3), (1, 5), (3, 6), (4, 1), (1, 4), (2, 1), (6, 5), (4, 6), (3, 1), (6, 2), (7, 3), (4, 7), (3, 0), (1, 6), (3, 7), (0, 3), (7, 2), (4, 0), (2, 0), (5, 7), (1, 1), (2, 7), (6, 6), (5, 0), (0, 4), (7, 5), (6, 1), (0, 2), (7, 4), (0, 5), (0, 7), (6, 7), (7, 6), (7, 7), (0, 0), (7, 1), (6, 0), (1, 0), (0, 1), (7, 0), (0, 6), (1, 7)]

I am amazed at how much code I got in there, for such a trivial problem.

My question is : can I make it faster and/or shorter ( use less temporaries/function calls )?