MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / valid

Function valid

projects/Sudoku-Solver/sudokutools.py:32–58  ·  view source on GitHub ↗

Whether a number is valid in that cell, returns a bool

(board, pos, num)

Source from the content-addressed store, hash-verified

30
31
32def 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
61def solve(board):

Callers 3

generateFunction · 0.90
visualSolveMethod · 0.90
solveFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected