Whether a number is valid in that cell, returns a bool
(board, pos, num)
| 30 | |
| 31 | |
| 32 | def valid(board, pos, num): |
| 33 | """Whether a number is valid in that cell, returns a bool""" |
| 34 | |
| 35 | for i in range(9): |
| 36 | if ( |
| 37 | board[i][pos[1]] == num and (i, pos[1]) != pos |
| 38 | ): # make sure it isn't the same number we're checking for by comparing coords |
| 39 | return False |
| 40 | |
| 41 | for j in range(9): |
| 42 | if ( |
| 43 | board[pos[0]][j] == num and (pos[0], j) != pos |
| 44 | ): # Same row but not same number |
| 45 | return False |
| 46 | |
| 47 | start_i = pos[0] - pos[0] % 3 # ex. 5-5%3 = 3 and thats where the grid starts |
| 48 | start_j = pos[1] - pos[1] % 3 |
| 49 | for i in range(3): |
| 50 | for j in range( |
| 51 | 3 |
| 52 | ): # adds i and j as needed to go from start of grid to where we need to be |
| 53 | if ( |
| 54 | board[start_i + i][start_j + j] == num |
| 55 | and (start_i + i, start_j + j) != pos |
| 56 | ): |
| 57 | return False |
| 58 | return True |
| 59 | |
| 60 | |
| 61 | def solve(board): |
no outgoing calls
no test coverage detected