Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:52:16+00:00 2026-05-18T08:52:16+00:00

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

  • 0

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 )?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:52:17+00:00Added an answer on May 18, 2026 at 8:52 am

    To make it shorter (and using a slightly different metric):

    >>> rows, cols, centerx, centery = 6, 6, 2.5, 2.5
    >>> [p[1:] for p in sorted((((x - centerx) ** 2 + (y - centery) ** 2, x, y)
    ...                         for x in xrange(rows) for y in xrange(cols)))]
    [(2, 2), (2, 3), (3, 2), (3, 3), (1, 2), (1, 3), 
     (2, 1), (2, 4), (3, 1), (3, 4), (4, 2), (4, 3), 
     (1, 1), (1, 4), (4, 1), (4, 4), (0, 2), (0, 3),
     (2, 0), (2, 5), (3, 0), (3, 5), (5, 2), (5, 3), 
     (0, 1), (0, 4), (1, 0), (1, 5), (4, 0), (4, 5),
     (5, 1), (5, 4), (0, 0), (0, 5), (5, 0), (5, 5)]
    

    To make it faster:

    • Don’t take square roots (as in my code above): sorting by the square of the distance is just as good as sorting by the distance, and taking square roots is relatively slow unnecessary.
    • Exploit the 8-way symmetry: sort one octant and copy it out 8 times.

    In the comments, PoorLuzer asks, “I also did not understand why you init centerx, centery = 2.5, 2.5.” I hope this figure makes it clear:

    the centre of a 6x6 grid with coordinates running from 0 to 5 on each axis is at 2.5,2.5

    PoorLuzer also wonders how come our metrics differ, given that we are both using the Euclidean distance formula. Well, my metric takes the distance from the centre of each square to the centre of the whole grid. For example, for these 8 cells the distance from the centre is √2.5 = about 1.58:

    distance to centre for eight cells is sqrt(2.5)

    Whereas PoorLuzer is taking the Euclidean distance to the closest of the four centre squares (and rounding it to an integer). For the same 8 cells PoorLuzer assigns a distance of 1:

    distance for same eight cells is 1 in PoorLuzer's metric

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the same question for Maven that someone had about Ant ( How
In previous question of mine, someone had meantioned that using Semaphores were expensive in
I had someone mention to me that catching all exceptions is not necessarily good
I just found out because someone had a ' in their last name and
I know it is a strange question did someone have had a case where
I updated my User class, and now whenever someone that had the old version
I had a working cake application that I tar'd up and sent to someone
As part of another question that did involve programming (I assure you), someone suggested
Someone had a problem with their code on a different site and I noticed
At any time in the past, if someone had asked me the maximum size

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.