[ create a new paste ] login | about

Link: http://codepad.org/IXDlZuUu    [ raw code | output | fork ]

Python, pasted on Apr 13:
correct = [[1,2,3],
           [2,3,1],
           [3,1,2]]

incorrect = [[4,1,2,3,6,7,8,5,1],
             [6,5,8,9,4,1,3,2,7],
             [3,9,7,5,2,8,1,6,4],
             [9,6,4,2,3,5,7,8,1],
             [7,3,5,1,8,4,6,9,2],
             [8,2,1,7,9,6,4,3,5],
             [1,4,9,6,5,3,2,7,8],
             [5,8,3,4,7,2,9,1,6],
             [2,7,6,8,1,9,5,4,3]]

incorrect2 = [[4,1,2,3,6,7,8,5,1,2],
             [6,5,8,9,4,1,3,2,7],
             [3,9,7,5,2,8,1,6,4],
             [9,6,4,2,3,5,7,8,1],
             [7,3,5,1,8,4,6,9,2],
             [8,2,1,7,9,6,4,3,5],
             [1,4,9,6,5,3,2,7,8],
             [5,8,3,4,7,2,9,1,6],
             [2,7,6,8,1,9,5,4,3]]


# Checks if Sudoku is valid
def check_sudoku(array):  
    is_sudoku = True
    reference = range(1, len(array) + 1)
    transposed = zip(array)
    
    for row, _ in zip(array, transposed):
        if sorted(row) != reference:
            is_sudoku = False
            break
    return is_sudoku

print check_sudoku(correct)
print check_sudoku(incorrect)
print check_sudoku(incorrect2)


Output:
1
2
3
True
False
False


Create a new paste based on this one


Comments: