Validate Sudoku Board

This function takes in a 2D array and determines if it is a valid Sudoku board. A Sudoku board is valid if each row, column, and 3x3 subarray contains exactly 9 integers.

Problem

Given a 2D array of integers, write a function that determines whether the array is a valid Sudoku board. A Sudoku board is valid if:
- Each row contains exactly 9 integers
- Each column contains exactly 9 integers
- Each 3x3 subarray (indicated by bold lines in the array below) contains exactly 9 integers
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]]
Output: true

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def is_valid_sudoku(board):
    # check rows
    for row in board:
        if not is_valid_row(row):
            return False
    # check columns
    for i in range(len(board)):
        column = [row[i] for row in board]
        if not is_valid_row(column):
            return False
    # check 3x3 subarrays
    for i in range(0, len(board), 3):
        for j in range(0, len(board), 3):
            subarray = [board[i][j], board[i][j+1], board[i][j+2], board[i+1][j], board[i+1][j+1], board[i+1][j+2], board[i+2][j], board[i+2][j+1], board[i+2][j+2]]
            if not is_valid_row(subarray):
                return False
    return True

def is_valid_row(row):
    return len(row) == 9 and len(set(row)) == 9 and all(0 < x < 10 for x in row)

board = [[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]]

print(is_valid_sudoku(board))

A.I. Evaluation of the Solution

The candidate's solution correctly solves the problem. The solution is complete, with functions to check rows, columns, and 3x3 subarrays. The candidate's approach is clear and easy to follow.

Evaluated at: 2022-11-09 08:16:07