I have a 9×9 sudoku board. I have to create 9 subsquare lists. I store each of these lists as a row in another list called sub_square.
CODE:
sub_square = []
sub_list = []
for x in range(0,3):
for y in range(0,3):
sub_list.append(sudoku[x][y])
sub_square.append(sub_list)
sub_list = []
for x in range (0,3):
for y in range(3,6):
sub_list.append(sudoku[x][y])
sub_square.append(sub_list)
sub_list = []
for x in range (0,3):
for y in range(6,9):
sub_list.append(sudoku[x][y])
sub_square.append(sub_list)
sub_list = []
I do the same for the other 6 sub squares. Is there any other way that is much simpler.
SUDOKU:
[0, 9, 4, 3, 0, 0, 0, 0, 0],
[0, 0, 7, 5, 0, 0, 0, 0, 0],
[0, 0, 1, 4, 0, 0, 0, 2, 0],
[4, 6, 0, 8, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 3, 0, 6, 9],
[0, 5, 0, 0, 0, 6, 2, 0, 0],
[0, 0, 0, 0, 0, 5, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 6, 4, 0]
so the first sub square list is:
[0, 9, 4, 0, 0, 7, 0, 0, 1]
Yes, there is an easier way:
Where
handwwould be the height and width of each subsquare, respectively. (For your purposes,handwwould both equal 3.