in step 43, you are asked to print the gameboard.value_in_square(1, 0, 3). My question is, how is row 1 the first 3 rows; why isn’t it row 0? Column 0 is the first 3 columns, which makes sense because (col // 3) * 3 should = 0 for 0, 1, and 2… how is this not the same for (row // 3 * 3)?
I’m not understanding your question. Can you please post a link to the step and any code you currently have?
Here’s the code (step 43)…
class Board:
def __init__(self, board):
self.board = board
def find_empty_cell(self):
for row, contents in enumerate(self.board):
try:
col = contents.index(0)
return row, col
except ValueError:
pass
return None
def valid_in_row(self, row, num):
return num not in self.board[row]
def valid_in_col(self, col, num):
return all(self.board[row][col] != num for row in range(9))
def valid_in_square(self, row, col, num):
row_start = (row // 3) * 3
col_start = (col // 3) * 3
for row_no in range(row_start, row_start + 3):
for col_no in range(col_start, col_start + 3):
if self.board[row_no][col_no] == num:
return False
return True
puzzle = [
[0, 0, 2, 0, 0, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 3, 7, 6, 2],
[4, 3, 0, 0, 0, 0, 8, 0, 0],
[0, 5, 0, 0, 3, 0, 0, 9, 0],
[0, 4, 0, 0, 0, 0, 0, 2, 6],
[0, 0, 0, 4, 6, 7, 0, 0, 0],
[0, 8, 6, 7, 0, 4, 0, 0, 0],
[0, 0, 0, 5, 1, 9, 0, 0, 8],
[1, 7, 0, 0, 0, 6, 0, 0, 5]
]
gameboard = Board(puzzle)
print(gameboard.valid_in_square(1, 0, 3))
In this code, the first square is being iterated through… but I thought the first square should have been (0, 0, num).
Can you please post a link to the step?
How familiar are you with Sudoku?
The first square is the 9 values in rows 0,1,2 and columns 0,1,2.
I’m familiar and I agree with your statement. My question is still the same. Why is it accessed as square(1, 0, num) and not square(0, 0, num)?
row_start = (row // 3) * 3, right? For rows 0,1,2, row_start = 0, right?
where do you see square(1, 0, num)
?
Okay, I get it now… the square is accessable using 0, 1, or 2 for both rows or columns… so square(1,2,num) is also the first square… and the 9th square can be accessed by square(8,7,num).
exactly this
in step 43, they tell you to print gameboard.valid_in_square(1,0,3). The point is to check for the (num)ber 3 in the upper left square. That square can be acessed from (0,0,num) through (2,2,num). The (1,0,num) threw me off for a minute, obviously.
oh, that is confusing, I was searching for a method or function called square
, instead you meant the one called valid_in_square
!
I just meant the first, upper left square.