Python Valid Sudoku Board Checker 9x9 validate squares but not showing output results

Viewed 415

I have been working on a task where I validate a already completed Sudoku puzzle and see if it has been done properly. I have been able to define the functions and able to section the code to different segments to allow the code to flow properly. I checked the rows,columns and the sub-matrix squares. The Sudoku puzzle is provided through a text file which is able to be read when running the program. When I run the program the only thing that displays is the Sudoku board. What do I have to do to show that the puzzle is valid ?

def main():
 fl =('sudoku board.txt')
 grids =(is_puzzle(fl))



def is_Puzzle(fl):
with open(fl,'r') as files:
    grids = []
    lines = files.readlines()
    for line in lines:
         board = line.split()
         wholenums = [int(items) for items in board]
         print (wholenums)
         grids.append(wholenums)
    return grids




def is_puzzle(fl):


if check_rows(fl) and check_columns(fl) and check_squares(fl):
    return True
else:
    return False

def compare_all(fl):
compare = [1,2,3,4,5,6,7,8,9]
return sorted(lst) `== compare


def check_rows(fl):
for i in grids:
    if not grids.compare_all(i):
        return False
return True




def check_columns(fl):
for i in range(len(fl)):
    contain = []
    for j in range(len(fl[0])):
        contain.append(fl[j][i])
    if not grids.compare_all(hold):
        return False


def check_squares(fl):
for i in range(0, 9, 3):
    for j in range(0, 9, 3):
        nums = fl[i][j:j+3]+fl[i+1][j:j+3]+fl[i+2][j:j+3]
        if not grids.compare_all(nums):
            return False
return True


main()
1 Answers

Here is your code fixed, and performs as expected. Besides a few tweaks, you have to check for the return value of is_puzzle() function.

def main():
    fl = 'sudoku board.txt'
    grids = (is_Puzzle(fl))
    print()
    if is_puzzle(grids):
        print('The sudoku is correct!')
    else:
        print('The sudoku is wrong...')

def is_Puzzle(fl):
    with open(fl, 'r') as files:
        grids = []
        lines = files.readlines()
        for line in lines:
            board = line.split()
            wholenums = [int(items) for items in board]
            print(wholenums)
            grids.append(wholenums)
        return grids

def is_puzzle(fl):
    if check_rows(fl) and check_columns(fl) and check_squares(fl):
        return True
    else:
        return False

def compare_all(fl):
    compare = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    return sorted(fl) == compare

def check_rows(fl):
    for i in fl:
        if not compare_all(i):
            return False
    return True

def check_columns(fl):
    for i in range(len(fl)):
        contain = []
        for j in range(len(fl[0])):
            contain.append(fl[j][i])
        if not compare_all(contain):
            return False
    return True

def check_squares(fl):
    for i in range(0, 9, 3):
        for j in range(0, 9, 3):
            nums = fl[i][j:j + 3] + fl[i + 1][j:j + 3] + fl[i + 2][j:j + 3]
            if not compare_all(nums):
                return False
    return True

main()
Related