Created
October 10, 2013 02:36
-
-
Save zh4ngx/6912155 to your computer and use it in GitHub Desktop.
Quick solution to sudoku checker. Does NOT check if board is correctly completable, only if it's currently correct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check nums for rows, columns and blocks | |
def check(nums): | |
num_dict = {} | |
# assume nums is a list of integers | |
# placeholder for empty is None | |
for num in nums: | |
if not num: | |
continue | |
if num in num_dict: | |
return false | |
else: | |
num_dict[num] = false | |
# Check rows for board[i][j] (2d array) | |
def row_check(board): | |
for row in board: | |
if not check(row): | |
return false | |
return true | |
# Check columns for board | |
def column_check(board): | |
for column in range(9): | |
#column is int | |
if not check([ row[column] for row in board ]): | |
return false | |
return true | |
# Check 3x3 blocks, index(0) by block_row, block_column | |
def block_check(board, block_row, block_col): | |
# accumulate list | |
#row is int | |
nums = [] | |
for row in range(3 * block_row, 3 * block_row + 3): | |
for column in range(3 * block_col, 3 * block_col + 3): | |
nums.append(board[row][column]) | |
if not check(nums): | |
return false | |
return true | |
def check_board(board): | |
if not row_check(board): | |
return false | |
if not column_check(board: | |
return false | |
for block_row in range(0,3): | |
for block_col in range(0,3): | |
if not block_check(board): | |
return false | |
return true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment