Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions problem-bank/judges.json
Original file line number Diff line number Diff line change
Expand Up @@ -3357,6 +3357,142 @@
]
],
"expected": true
},
{
"label": "duplicate in last column only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "1"],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
},
{
"label": "duplicate in last row only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
["9", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
Comment thread
RyanJ2002 marked this conversation as resolved.
Resolved
},
{
"label": "duplicate in far box only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "7"],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
},
{
"label": "consistent but unsolvable",
"input": [
[
["5", "3", "1", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": true
},
{
"label": "fully filled board",
"input": [
[
["5", "3", "4", "6", "7", "8", "9", "1", "2"],
["6", "7", "2", "1", "9", "5", "3", "4", "8"],
["1", "9", "8", "3", "4", "2", "5", "6", "7"],
["8", "5", "9", "7", "6", "1", "4", "2", "3"],
["4", "2", "6", "8", "5", "3", "7", "9", "1"],
["7", "1", "3", "9", "2", "4", "8", "5", "6"],
["9", "6", "1", "5", "3", "7", "2", "8", "4"],
["2", "8", "7", "4", "1", "9", "6", "3", "5"],
["3", "4", "5", "2", "8", "6", "1", "7", "9"]
]
],
"expected": true
},
{
"label": "duplicate in first column only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
["4", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
},
{
"label": "duplicate in first row only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "7"],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
},
{
"label": "duplicate in edge box only",
"input": [
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", "7", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]
],
"expected": false
}
],
"paramNames": [
Expand Down
110 changes: 110 additions & 0 deletions tests/browser/compiler-explorer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,116 @@ test("stock III cases reject the two-runs shortcut and its greedy join", () => {
assert.equal(joined(true)([0, 1, 0, 3, 1, 4]), 6);
});

test("valid-sudoku cases reject a check that misses a rule or a unit", () => {
const all = ["row", "column", "box"];
// Records each filled cell under the units its `rules` name and refuses a
// digit a unit has already seen. A wrong check leaves out a rule, the rows
// and columns outside `from` to `to`, or the boxes, numbered in reading
// order, that `boxes` does not list.
const consistent =
({ rules = all, from = 0, to = 9, boxes = [...Array(9).keys()] } = {}) =>
(board) => {
const seen = new Set();
const inside = (index) => index >= from && index < to;
for (let r = 0; r < 9; r += 1) {
for (let c = 0; c < 9; c += 1) {
const digit = board[r][c];
if (digit === ".") continue;
const box = 3 * Math.floor(r / 3) + Math.floor(c / 3);
const units = {
row: inside(r) ? `row ${r}` : "",
column: inside(c) ? `column ${c}` : "",
box: boxes.includes(box) ? `box ${box}` : "",
};
for (const rule of rules) {
if (!units[rule]) continue;
const key = `${units[rule]} ${digit}`;
if (seen.has(key)) return false;
seen.add(key);
}
}
}
return true;
};
const every = consistent();
for (const { label, input, expected } of judges["valid-sudoku"].cases) {
Comment thread
RyanJ2002 marked this conversation as resolved.
Resolved
assert.equal(
every(...structuredClone(input)),
expected,
`"${label}" must expect whether any row, column or box repeats a digit`,
);
}
// Each of these cases repeats a digit in one unit only, so the check that
// misses that unit is the one wrong check that accepts it. The repeats sit
// in the first and last row and column, in the last box and in a box off
// the diagonal and the corners, so loops that start or stop one short, or
// step through the boxes by three, are refused too.
for (const [label, wrong, misses] of [
["duplicate in box only", { rules: ["row", "column"] }, "the box rule"],
["duplicate in far box only", { rules: ["row", "column"] }, "the box rule"],
["duplicate in far box only", { boxes: [0] }, "all but the top-left box"],
[
"duplicate in edge box only",
{ rules: ["row", "column"] },
"the box rule",
],
[
"duplicate in edge box only",
{ boxes: [0, 4, 8] },
"the boxes off the diagonal",
],
[
"duplicate in edge box only",
{ boxes: [0, 2, 6, 8] },
"all but the corner boxes",
],
["duplicate in last column only", { rules: ["row", "box"] }, "columns"],
["duplicate in last column only", { to: 8 }, "the last row and column"],
["duplicate in last row only", { rules: ["column", "box"] }, "rows"],
["duplicate in last row only", { to: 8 }, "the last row and column"],
["duplicate in first column only", { rules: ["row", "box"] }, "columns"],
["duplicate in first column only", { from: 1 }, "the first row and column"],
["duplicate in first row only", { rules: ["column", "box"] }, "rows"],
["duplicate in first row only", { from: 1 }, "the first row and column"],
]) {
assertRejects(
"valid-sudoku",
label,
consistent(wrong),
`"${label}" must reject a check that skips ${misses}`,
);
}
// No other true case is filled in, so a check that wants a blank cell
// passed them all.
assertRejects(
"valid-sudoku",
"fully filled board",
(board) => board.some((row) => row.includes(".")) && every(board),
'"fully filled board" must reject a check that requires a blank cell',
);
// Fills the first blank cell with each digit the board still allows and
// backtracks, so it finds a completion if one exists.
const solvable = (board) => {
const r = board.findIndex((row) => row.includes("."));
if (r < 0) return true;
const c = board[r].indexOf(".");
return [..."123456789"].some((digit) => {
board[r][c] = digit;
const found = every(board) && solvable(board);
board[r][c] = ".";
return found;
});
};
// Both other true cases can be completed, so a check that also asks for a
// completion passed them all.
assertRejects(
"valid-sudoku",
"consistent but unsolvable",
(board) => every(board) && solvable(board),
'"consistent but unsolvable" must reject a check that requires a completion',
);
});

test("harness generator covers every function problem without network access", () => {
for (const [id, spec] of Object.entries(judges)) {
if (spec.kind !== "function") continue;
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/task-posed.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"valid-anagram": "7b5c1cd037cfef4b4d5d7dddb2d41d6bc0e19923d918d30bd42af4b8f1ff1f70",
"valid-palindrome": "c099c915b26ba3c046b970b6f6689c379734633849b221d0e3f4956fac0c5660",
"valid-parentheses": "bd402fe8dc9b2ad343d170fefb39c86e2d7c241a6c706be16c62d99f6d22a37c",
"valid-sudoku": "15fe9371f477e85dd5b9cc14d4a509c9521aeb7720304361248c427e2fc10c63",
"valid-sudoku": "5f49d34894e9c8eecf3eb39c468cdd9a3ae061cb76857ab843c6dab876b1ba08",
"validate-binary-search-tree": "915a3962ecb3712bda3cd7732da42541ec01580b541d188eb09b6428f7eb9356",
"word-break": "09bb149cf1f57599f4100dda9f14cc3c32c86ddda75e7c226c532c027e782b8b",
"word-ladder": "3e1f5f8ca8fdeeda16dc56234eefa344dee787a2dd163c6067d947e7350b6171",
Expand Down
8 changes: 8 additions & 0 deletions tests/web/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,14 @@ fn static_problem_bank_and_judges_cover_each_problem() {
),
("minimum-size-subarray-sum", "no qualifying window"),
("valid-sudoku", "duplicate in box only"),
("valid-sudoku", "duplicate in last column only"),
("valid-sudoku", "duplicate in last row only"),
("valid-sudoku", "duplicate in first column only"),
("valid-sudoku", "duplicate in first row only"),
("valid-sudoku", "duplicate in far box only"),
("valid-sudoku", "duplicate in edge box only"),
("valid-sudoku", "consistent but unsolvable"),
("valid-sudoku", "fully filled board"),
("spiral-matrix", "single column"),
("rotate-image", "negative values"),
("set-matrix-zeroes", "first column marker"),
Expand Down
Loading
Loading