Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Bounty Awarded with 50 reputation awarded by Lin Ma
deleted 4 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
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 and square_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 the square_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
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 and square_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 the square_map dictionary)
  • organize imports per PEP8 (reference)
  • two blank lines between the functions (reference)
deleted 4 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93

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):

added 48 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Loading
added 48 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Loading
added 48 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Loading
added 65 characters in body
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Loading
Source Link
alecxe
  • 17.5k
  • 8
  • 52
  • 93
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /