#### Is the 2D Array a Valid Sudoku Puzzle?

This function returns true if the given 2D array is a valid Sudoku puzzle, and false otherwise. A valid Sudoku puzzle is one in which each row, column, and 3x 3 sub-array contains all of the integers from 1 to 9 with no repeats.

#### Problem

Given a 2D array of integers, write a function that returns true if the array is a valid Sudoku puzzle, and false otherwise.
A valid Sudoku puzzle is one in which each row, column, and 3x3 sub-array contains all of the integers from 1 to 9 with no repeats.
Example 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]]
Example output : true

#### Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def is_valid_sudoku(puzzle):
# check rows
for row in puzzle:
if not is_valid_row(row):
return False

# check columns
for i in range(len(puzzle)):
column = [row[i] for row in puzzle]
if not is_valid_row(column):
return False

# check 3x3 sub-arrays
for i in range(0, len(puzzle), 3):
for j in range(0, len(puzzle), 3):
sub_array = [puzzle[i][j], puzzle[i][j+1], puzzle[i][j+2],
puzzle[i+1][j], puzzle[i+1][j+1], puzzle[i+1][j+2],
puzzle[i+2][j], puzzle[i+2][j+1], puzzle[i+2][j+2]]
if not is_valid_row(sub_array):
return False

return True

def is_valid_row( row):
return sorted(row) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

puzzle = [[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(puzzle))

A.I. Evaluation of the Solution

This is a great solution! The candidate has thought about the problem and broken it down into smaller pieces that are easier to solve. The candidate has also written helper functions to make the code more readable.

Evaluated at: 2022-11-24 02:16:17