You are on page 1of 2

import math

#
#
#
#
#
#
#
#
#

The task for this part of the assignment is to write a Python


application called sudokuchecker.py that will read in a partially-solved
Sudoku puzzle from a text file and check to see if it contains valid
digits. If the same number occurs twice in a row, column, or block, the
puzzle is invalid. The text file will only contain the puzzle, with
blanks represented by zeroes, and no other lines or data. Ask the user
to enter the filename of the input file, and then output whether or not
the puzzle is valid. You may assume that the input file given will
contain a puzzle

def string_to_list(string):
number_list = []
for item in string:
number_list.append(int(item))
return number_list
def build_sudoku(filename):
sudoku = []
for line in open(filename):
# split line so each line is a list
sudoku.append(string_to_list(line.strip('\n')))
return sudoku
def check_list(list):
# check to see if each item in the list matches any other item later in the
# list. If a list item does match, return false
for index, item in enumerate(list):
if item and item in list[index+1:]:
return False
return True
def build_column(index, matrix):
test_set = []
for row in matrix:
test_set.append(row[index])
return test_set
def build_blocks(matrix):
# Build a list of blocks of n x n size from a passed in matrix
# this block is lazily built, in that the data structure looks like
# [n1, n2, ...nf] instead of [[n1, n2, n3], [n4...nf], ... []]
# Each block is a single list instead of being a list of list, this
# makes it easier to parse
block_size = int(math.sqrt(len(matrix)))
blocks = []
block_row = 0
for block_number in range(len(matrix)):
# create n "blocks" for size of matrix
# blocks will be the size of the matrix. If a matrix is contains 9
# lists the block_size will be 3 x 3, or 9
block = []
if block_number % block_size == 0:

# increase block_row by 1
block_row += 1
start = (block_number % block_size) * block_size
finish = start + block_size
for row in range(block_size):
index = ((block_row-1)*3)+row
block.extend(matrix[(index)][start:finish])
blocks.append(block)
return blocks
def valid_matrix(matrix):
# checks to make sure the matrix is of a symmetrical size
length = len(matrix)
# Is the matrix a perfect square?
sqrt_of_length = math.sqrt(length)
if length != sqrt_of_length * sqrt_of_length:
return False
# Are all the rows the length of the matrix?
for row in matrix:
if len(row) != length:
return False
return True
def validate_sudoku(matrix):
# takes in a sudoku matrix and returns true if it is a valid sudoku and
# false otherwise
if not valid_matrix(matrix):
return False
sudoku_block = build_blocks(matrix)
for index, row in enumerate(matrix):
# check the column, row and block versions of the list at the same time
column = build_column(index, matrix)
if (not check_list(row) or not check_list(column) or not
check_list(sudoku_block[index])):
return False
return True
if __name__ == "__main__":
filename = input("What is the name of the sudoku file?: ")
sudoku = build_sudoku(filename)
valid = validate_sudoku(sudoku)
if valid:
print("The puzzle is valid!")
else:
print("The puzzle is not valid!")