I need help creating a list for each of the 9 3x3 blocks in sudoku. so I have a list of lists representing the original sudoku board (zero means empty):
board=[[2,0,0,0,0,0,0,6,0],
[0,0,0,0,7,5,0,3,0],
[0,4,8,0,9,0,1,0,0],
[0,0,0,3,0,0,0,0,0],
[3,0,0,0,1,0,0,0,9],
[0,0,0,0,0,8,0,0,0],
[0,0,1,0,2,0,5,7,0],
[0,8,0,7,3,0,0,0,0],
[0,9,0,0,0,0,0,0,4]]
I need to turn these into a list of lists containing the 3x3 blocks. So for example:
[[2,0,0,0,0,0,0,4,8],[etc]]
i tried creating one list called "blocks" containing 9 other lists with just zeroes in each list. so it looked like:
blocks=[[0,0,0,0,0,0,0,0,0],[etc]
then i used a while loop to change the values in the list:
BLOCK_COUNT=0
BOARD_COUNT=0
while BLOCK_COUNT<len(blocks):
blocks[BLOCK_COUNT][0]=board[BOARD_COUNT][BOARD_COUNT]
blocks[BLOCK_COUNT][1]=board[BOARD_COUNT][BOARD_COUNT+1]
blocks[BLOCK_COUNT][2]=board[BOARD_COUNT][BOARD_COUNT+2]
blocks[BLOCK_COUNT][3]=board[BOARD_COUNT+1][BOARD_COUNT]
blocks[BLOCK_COUNT][4]=board[BOARD_COUNT+1][BOARD_COUNT+1]
blocks[BLOCK_COUNT][5]=board[BOARD_COUNT+1][BOARD_COUNT+2]
blocks[BLOCK_COUNT][6]=board[BOARD_COUNT+2][BOARD_COUNT]
blocks[BLOCK_COUNT][7]=board[BOARD_COUNT+2][BOARD_COUNT+1]
blocks[BLOCK_COUNT][8]=board[BOARD_COUNT+2][BOARD_COUNT+2]
BLOCK_COUNT+=1
BOARD_COUNT+=3
This however gives me an index error. if I create 2 of those while loops with "BLOCK_COUNT" being 3 and 6 respectively then i get a better answer but it still doesn't give me the correct 3x3 block for some. So i'm pretty much at a loss for how to do this. Thanks.
should work, in python3 you might replace "(m/3)*3" with "int(m/3)*3"
this uses no builtins and is faster 3 nested for loops
Of course, you could replace the whole thing with just one list comprehension:
In case you are interested in a version that doesn't use any built-ins to do any heavy lifting:
So what's happening here?:
Well, first, we decide to iterate over the 9 blocks that we want. These are governed by the
r
andc
variables. This is also why we multiply them by 3 when we access the numbers on the board (because each block is a square of side 3).Next, we want to iterate over the elements in each block. Translation: Lookup the numbers within each 3x3 block. The index of each element within the block is governed by
i
andj
. So we havei
andj
that govern the elements we want to access, along withr
andc
, which are their offsets from the board itself, determining the location of the "block" we want. Now we're off to the races.For each
r
andc
(notice that each loops overrange(3)
, so there are 9(r,c)
pairs - the 9 blocks that we are after), loop over the 9 elements in the block (the 9(i,j)
pairs). Now, simply access the elements based on their relative locations from the(r,c)
offsets (3*r
gives the first row of the relevant block, and addingi
gives the row of the required element. Similarly,3*c
gives the first column of the relevant block, and addingj
gives the column of the required element. Thus, we have the coordinates of the element we want). Now, we add the element toblock
.Once we've looped over all the elements in the block, we add the block itself to the answer, and presto! we're done
You can do this with a combination of
reshape
andtranspose
when you usenumpy
.edit - sorry - hit enter too soon:
Output: