1 year ago

#342359

test-img

Zoltán Orosz

Get all the subgrids from a Sudoku board using only NumPy methods

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)

python

numpy

multidimensional-array

indexing

sudoku

0 Answers

Your Answer

Accepted video resources