Note the use of _
for throw-away variables _
for throw-away variables.
Note the use of _
for throw-away variables.
Note the use of _
for throw-away variables.
from itertools import chain
def is_valid_block(block):
"""Returns true if a block (row, column or square) is a valid Sudoku block."""
return len(block) == 9 and sum(block) == sum(set(block))
from itertools import chain
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
# check each row
for row in matrix:
if not is_valid_block(row):
return False
# check each column
for col in zip(*matrix):
if not is_valid_block(col):
return False
# check each 3x3 square
for i in range(0, 9, 3):
for j in range(0, 9, 3):
square = list(chain(row[j:j + 3] for row in matrix[i:i + 3]))
if not is_valid_block(square):
return False
return True
- use pretty-printing via
pprint
instead of a regular print - unused
square_x
andsquare_y
variables in the "main" part of the script (that might be actually an error/bug, since, I was expecting you to use them to make a key for thesquare_map
dictionary) - organize imports per PEP8 (reference)
- two blank lines between the functions (reference)
from itertools import chain
def is_valid_block(block):
"""Returns true if a block (row, column or square) is a valid Sudoku block."""
return len(block) == 9 and sum(block) == sum(set(block))
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
# check each row
for row in matrix:
if not is_valid_block(row):
return False
# check each column
for col in zip(*matrix):
if not is_valid_block(col):
return False
# check each 3x3 square
for i in range(0, 9, 3):
for j in range(0, 9, 3):
square = list(chain(row[j:j + 3] for row in matrix[i:i + 3]))
if not is_valid_block(square):
return False
return True
- use pretty-printing via
pprint
instead of a regular print - unused
square_x
andsquare_y
variables in the "main" part of the script - organize imports per PEP8 (reference)
- two blank lines between the functions (reference)
def is_valid_block(block):
"""Returns true if a block (row, column or square) is a valid Sudoku block."""
return len(block) == 9 and sum(block) == sum(set(block))
from itertools import chain
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
# check each row
for row in matrix:
if not is_valid_block(row):
return False
# check each column
for col in zip(*matrix):
if not is_valid_block(col):
return False
# check each 3x3 square
for i in range(0, 9, 3):
for j in range(0, 9, 3):
square = list(chain(row[j:j + 3] for row in matrix[i:i + 3]))
if not is_valid_block(square):
return False
return True
- use pretty-printing via
pprint
instead of a regular print - unused
square_x
andsquare_y
variables in the "main" part of the script (that might be actually an error/bug, since, I was expecting you to use them to make a key for thesquare_map
dictionary) - organize imports per PEP8 (reference)
- two blank lines between the functions (reference)
We can also take it a step further and make use of generators and anyall()
:
def generate_blocks(matrix):
"""Generates rows, columns and 3x3 squares for a Sudoku matrix."""
for row in matrix:
yield row
for col in zip(*matrix):
yield col
for i in range(0, 9, 3):
for j in range(0, 9, 3):
yield list(chain(*(row[j:j + 3] for row in matrix[i:i + 3])))
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
return anyall(not is_valid_block(block) for block in generate_blocks(matrix))
I have never written a sudoku solver before, but here is how I would think about implementing itthe brute-force solution (I guess that is just a different approach and it's not related to the code review):
We can also take it a step further and make use of generators and any()
:
def generate_blocks(matrix):
"""Generates rows, columns and 3x3 squares for a Sudoku matrix."""
for row in matrix:
yield row
for col in zip(*matrix):
yield col
for i in range(0, 9, 3):
for j in range(0, 9, 3):
yield list(chain(*(row[j:j + 3] for row in matrix[i:i + 3])))
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
return any(not is_valid_block(block) for block in generate_blocks(matrix))
I have never written a sudoku solver before, but here is how I would think about implementing it:
We can also take it a step further and make use of generators and all()
:
def generate_blocks(matrix):
"""Generates rows, columns and 3x3 squares for a Sudoku matrix."""
for row in matrix:
yield row
for col in zip(*matrix):
yield col
for i in range(0, 9, 3):
for j in range(0, 9, 3):
yield list(chain(*(row[j:j + 3] for row in matrix[i:i + 3])))
def is_valid_sudoku(matrix):
"""Returns True if a Sudoku matrix is valid, False otherwise."""
return all(is_valid_block(block) for block in generate_blocks(matrix))
I have never written a sudoku solver before, but here is how I would think about implementing the brute-force solution (I guess that is just a different approach and it's not related to the code review):