I have just started to learn programming with Python. I have been going through several online classes for the last month or two. Please bear with me if my questions are noobish.
One of the classes I am taking asked us to solve this problem.
# Define a procedure, check_sudoku,
# that takes as input a square list
# of lists representing an n x n
# sudoku puzzle solution and returns the boolean
# True if the input is a valid
# sudoku square and returns the boolean False
# otherwise.
# A valid sudoku square satisfies these
# two properties:
# 1. Each column of the square contains
# each of the whole numbers from 1 to n exactly once.
# 2. Each row of the square contains each
# of the whole numbers from 1 to n exactly once.
# You may assume the the input is square and contains at
# least one row and column.
After a while of trying to come up with the best code I can, this is what I ended up with.
def check_sudoku(sudlist):
x = range(1, len(sudlist)+1) # makes a list of each number to be found
rows = [[row[i] for row in sudlist] for i in range(len(sudlist))] # assigns all the rows to a flat list
z = range(len(sudlist))
for num in x:
for pos in z:
if num not in sudlist[pos] or num not in rows[pos]:
return False
return True
This code does work and passed all checks the class asked for. I just wonder what problems could the code have or how would you improve it?
I am just trying to improve by getting input from others. I appreciate any suggestions.