1

I have to write a SudokuBoard class using only NumPy. As part of this class I want to define the get_block_idx_with_highest_sum() function which will return the index of the subgrid (3x3) which has the highest sum of containing numbers.

For example:

The board:

[[0, 1, 2, 3, 4, 5, 6, 7, 8],
 [1, 2, 3, 4, 5, 6, 7, 8, 9],
 [2, 3, 4, 5, 6, 7, 8, 9, 0],
 [3, 4, 5, 6, 7, 8, 9, 0, 1],
 [4, 5, 6, 7, 8, 9, 0, 1, 2],
 [5, 6, 7, 8, 9, 0, 1, 2, 3],
 [6, 7, 8, 9, 0, 1, 2, 3, 4],
 [7, 8, 9, 0, 1, 2, 3, 4, 5],
 [8, 9, 0, 1, 2, 3, 4, 5, 6]]

In this case the sums of the subgrids:

(0,0) = 18; (0,1) = 45; (0,2) = 62

(1,0) = 45; (1,1) = 62; (1,2) = 19

(2,0) = 62; (2,1) = 19; (2,2) = 36

So the (0,2), (1,1) or (2,0) subgrid has the highest sum.

I want to get the subgrids from the board, but when I use a numpy array to indexing the 2d numpy array (board) I get this error:

only integer scalar arrays can be converted to a scalar index

How can I get all the subgrids using only numpy methods?

The (wrong) function:

def get_block_idx_with_highest_sum(self):
        indices = np.arange(9)
        sub_board_sum_values = self.table[indices//3:indices//3+3, indices%3:indices%3]
        return sub_board_sum_values

(The function returns the sub_board_sum_values, but it just for testing)

Zoltán Orosz
  • 303
  • 1
  • 8
  • What is your expected result? – Michael Szczesny Mar 27 '22 at 19:59
  • @MichaelSzczesny can you explain the logic of the code (I just started to learn NumPy so it will be very helpful) and post as an aswer so I can mark as the solution? It's working so thanks a lot! – Zoltán Orosz Mar 27 '22 at 20:13
  • 1
    Does this help? [Slice 2d array into smaller 2d arrays](https://stackoverflow.com/questions/16856788/slice-2d-array-into-smaller-2d-arrays). There are several similar questions with detailed answers on stackoverflow. – Michael Szczesny Mar 27 '22 at 20:21

0 Answers0