Mini Sudoku audit
EasyOnline Assessment
Audit a partly filled mini Sudoku and list every cell that breaks a rule.
The board is an n by n grid where n is 1, 4, 9 or 16, so it divides evenly into square boxes of side b = sqrt(n) (a 9 by 9 board has nine 3 by 3 boxes). A cell holds 0 when it is blank, or a number that is supposed to be between 1 and n.
A cell is flagged when either of these is true:
- Its number is outside
1..n(blank0is fine, but-1orn + 1is not). - Its number is valid, and the same number also appears in at least one other cell of its row, its column, or its box. Every cell involved in a clash is flagged, not only the later one.
Blanks never clash with anything.
Input: grid, a list of n rows, each a list of n integers.
Output: the flagged cells as [row, column] pairs (zero-based), in row-major order (top row first, left to right). Return an empty list if the board has no problems.
Example 1
- Input:
- [[[1,1,0,0],[0,0,0,0],[0,0,3,0],[0,0,0,3]]]
- Output:
- [[0,0],[0,1],[2,2],[3,3]]
- Explanation
- A 4 by 4 board has 2 by 2 boxes. Both 1s in row 0 clash, so
[0, 0]and[0, 1]are flagged. The 3 at[2, 2]and the 3 at[3, 3]are in the same bottom-right box, so both are flagged. Nothing else repeats.
Example 2
- Input:
- [[[0,0,0,2],[0,5,0,0],[4,0,4,0],[0,0,0,2]]]
- Output:
- [[0,3],[1,1],[2,0],[2,2],[3,3]]
- Explanation
- The 5 at
[1, 1]is out of range for a 4 by 4 board, so it is flagged. The 4s at[2, 0]and[2, 2]share row 2, so both are flagged. The 2s at[0, 3]and[3, 3]share column 3, so both are flagged. The zeros are blanks and never clash. Result in row-major order:[[0, 3], [1, 1], [2, 0], [2, 2], [3, 3]].
Example 3
- Input:
- [[[1]]]
- Output:
- []
- Explanation
- A single cell board has box side 1. The only valid number is 1, and a lone 1 clashes with nothing, so the answer is empty.
Constraints
gridisnbynwithnin {1, 4, 9, 16}-20 <= grid[r][c] <= 20