I’m working on an assignment for my CIS class in python. We have to code a Sudoku checker. In a 9×9 board we obviously have to check each row, col and 3×3 square for duplicates. I’m a little stuck on the idea of how to check the numbers by a 3×3 square. Below is my code for checking each row and col, if someone could help me a little with an outline or an approach just something for checking each 3×3 square that would be amazing!
self.columns = [ ]
for col in range(9):
col_tiles = [ ]
self.columns.append(col_tiles)
for row in range(9):
col_tiles.append(self.tiles[row][col])
self.squares = [ ]
for col in range(1, 10, 3):
for row in range(1, 10, 3):
square_tiles = [ ]
self.squares.append(square_tiles)
for x in range(3):
for y in range(3):
square_tiles.append(self.tiles[x][y])
This assumes you have the freedom to read the data and structure how you want. We want a set of unique values 1-9 for each row/column/3×3 grid, so one way is to either use a
setor a list comparison (we’ll usesethere to make it cleaner). If we create a set equal to the numbers from 1 to 9, we have a point against which we can compare all of our other groups. Assume a structure like this (from here):Where each row represents a full row of data. Now lets create a section of that data that represents the first three rows, pull out one grid and compare the contents to our set:
This iterates over the first three rows and returns the first three elements in each of those rows. To get a better visual, here is the equivalent
for-loop code to make it a bit easier to decipher:Since our set of numbers includes everything from 1-9, we know it is valid. To move to the second, we range the
row[:3]torow[3:6](androw[6:9]after that). You’ll then need to handle this for the next two sections as well. I’ll leave it to you as to how to wrap this in a more dynamic structure (note the multiples of three), but hopefully this will get you started 🙂