{"record":{"id":"66b41a85d93fbbfb","repo":"TheAlgorithms/Python","slug":"sudoku-boards-must-be-num-squares-x-num-squares","errorCode":null,"errorMessage":"Sudoku boards must be {NUM_SQUARES}x{NUM_SQUARES} squares.","messagePattern":"Sudoku boards must be (.+?)x(.+?) squares\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"matrix/validate_sudoku_board.py","lineNumber":132,"sourceCode":"    ... ,[\"9\",\"7\",\"8\",\"3\",\"1\",\"2\",\"4\",\"5\",\"6\"]\n    ... ])\n    True\n    >>> is_valid_sudoku_board([[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]])\n    Traceback (most recent call last):\n        ...\n    ValueError: Sudoku boards must be 9x9 squares.\n    >>> is_valid_sudoku_board(\n    ...        [[\"1\"], [\"2\"], [\"3\"], [\"4\"], [\"5\"], [\"6\"], [\"7\"], [\"8\"], [\"9\"]]\n    ...  )\n    Traceback (most recent call last):\n        ...\n    ValueError: Sudoku boards must be 9x9 squares.\n    \"\"\"\n    if len(sudoku_board) != NUM_SQUARES or (\n        any(len(row) != NUM_SQUARES for row in sudoku_board)\n    ):\n        error_message = f\"Sudoku boards must be {NUM_SQUARES}x{NUM_SQUARES} squares.\"\n        raise ValueError(error_message)\n\n    row_values: defaultdict[int, set[str]] = defaultdict(set)\n    col_values: defaultdict[int, set[str]] = defaultdict(set)\n    box_values: defaultdict[tuple[int, int], set[str]] = defaultdict(set)\n\n    for row in range(NUM_SQUARES):\n        for col in range(NUM_SQUARES):\n            value = sudoku_board[row][col]\n\n            if value == EMPTY_CELL:\n                continue\n\n            box = (row // 3, col // 3)\n\n            if (\n                value in row_values[row]\n                or value in col_values[col]\n                or value in box_values[box]","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/matrix/validate_sudoku_board.py#L114-L150","documentation":"Raised by is_valid_sudoku_board when the board is not exactly 9 rows of 9 cells each (NUM_SQUARES = 9). The validator checks outer length and every row's length before examining cell contents, so any 8x9, 9x10, or ragged board fails immediately. Content errors (bad values, duplicates) raise different errors later; this one is purely about geometry.","triggerScenarios":"is_valid_sudoku_board([[\"1\"]] * 8) (8 rows), a 9-row board where one row has 10 entries, boards built from strings without splitting into 9 characters, or nested lists flattened by a serialization step. The doctest itself shows [[\"1\"], [\"2\"], ...] (nine 1-element rows) raising.","commonSituations":"Parsing a puzzle string like \"53..7....\" and passing it as one flat list/row instead of 9 rows of 9; missing or extra cells from OCR or manual entry; boards loaded from CSV with a truncated line; test fixtures with copy-paste errors.","solutions":["Slice the puzzle into rows of nine: board = [puzzle[i:i+9] for i in range(0, 81, 9)] before validating.","If parsing digit strings, split each line into characters: [list(line) for line in lines] and verify you got exactly 9 lines.","Add a quick assertion len(board) == 9 and all(len(r) == 9 for r in board) with row indices printed to locate the malformed row.","Fix the source data: pad with the EMPTY_CELL marker or trim the stray cell so every row is exactly 9."],"exampleFix":"# before\nboard = list(\"53..7....534.67...\")  # flat 81-char list -> ValueError\nis_valid_sudoku_board(board)\n\n# after\nboard = [list(puzzle[i:i + 9]) for i in range(0, 81, 9)]\nis_valid_sudoku_board(board)","handlingStrategy":"validation","validationCode":"def is_9x9_board(board) -> bool:\n    return (\n        isinstance(board, list) and len(board) == 9\n        and all(isinstance(row, list) and len(row) == 9 for row in board)\n    )\n\nif not is_9x9_board(board):\n    raise ValueError(\"board must be 9 lists of 9 cells\")\nresult = is_valid_sudoku_board(board)","typeGuard":"def is_sudoku_shape(board) -> bool:\n    \"\"\"Guard: list of exactly 9 rows, each a list of exactly 9 items.\"\"\"\n    return (\n        isinstance(board, list)\n        and len(board) == 9\n        and all(isinstance(r, list) and len(r) == 9 for r in board)\n    )","tryCatchPattern":"try:\n    ok = is_valid_sudoku_board(board)\nexcept ValueError as e:\n    if \"9x9\" in str(e):\n        board = [board[i:i + 9] for i in range(0, 81, 9)]  # reshape flat input\n        ok = is_valid_sudoku_board(board)\n    else:\n        raise","preventionTips":["Reshape 81-cell flat inputs into 9 rows of 9 before validating.","When parsing puzzle strings, verify line count (9) and characters per line (9) at read time.","Include malformed boards (8 rows, 10 cells) in test fixtures so geometry bugs surface early."],"tags":["matrix","sudoku","validation","valueerror","board-geometry"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}