Coverage for pyguymer3/checkSudokuBoard.py: 5%
21 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def checkSudokuBoard(
5 board,
6 /,
7):
8 """Check a Sudoku board is valid
10 This function reads in a 2D array representing the values in a Sudoku board
11 and check that it is valid.
13 Parameters
14 ----------
15 board : numpy.ndarray
16 the Sudoku board
18 Returns
19 -------
20 valid : bool
21 the validity of the Sudoku board
23 Notes
24 -----
25 Copyright 2017 Thomas Guymer [1]_
27 References
28 ----------
29 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
30 """
32 # Import special modules ...
33 try:
34 import numpy
35 except:
36 raise Exception("\"numpy\" is not installed; run \"pip install --user numpy\"") from None
38 # Check arguments ...
39 if not isinstance(board, numpy.ndarray):
40 raise TypeError("\"board\" is not a NumPy array") from None
41 if board.shape != (9, 9):
42 raise TypeError("\"board\" is not the correct shape") from None
44 # Loop over numbers ...
45 for num in range(1, 10):
46 # Loop over columns ...
47 for iy in range(9):
48 # Check that the number is not in the row more than once ...
49 if (board[iy, :] == num).sum() > 1:
50 # Return answer ...
51 return False
53 # Loop over rows ...
54 for ix in range(9):
55 # Check that the number is not in the column more than once ...
56 if (board[:, ix] == num).sum() > 1:
57 # Return answer ...
58 return False
60 # Loop over y-squares ...
61 for sy in range(3):
62 # Loop over x-squares ...
63 for sx in range(3):
64 # Check that the number is not in the square more than once ...
65 if (board[sy * 3:(sy + 1) * 3, sx * 3:(sx + 1) * 3] == num).sum() > 1:
66 # Return answer ...
67 return False
69 # Return answer ...
70 return True