Skip to content

Remove redundent function in Backtracking Sudoku #4499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions backtracking/sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,6 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
return True


def is_completed(grid: Matrix) -> bool:
"""
This function checks if the puzzle is completed or not.
it is completed when all the cells are assigned with a non-zero number.

>>> is_completed([[0]])
False
>>> is_completed([[1]])
True
>>> is_completed([[1, 2], [0, 4]])
False
>>> is_completed([[1, 2], [3, 4]])
True
>>> is_completed(initial_grid)
False
>>> is_completed(no_solution)
False
"""
return all(all(cell != 0 for cell in row) for row in grid)


def find_empty_location(grid: Matrix) -> Optional[Tuple[int, int]]:
"""
This function finds an empty location so that we can assign a number
Expand Down Expand Up @@ -111,12 +90,7 @@ def sudoku(grid: Matrix) -> Optional[Matrix]:
>>> sudoku(no_solution) is None
True
"""

if is_completed(grid):
return grid

location = find_empty_location(grid)
if location is not None:
if location := find_empty_location(grid):
row, column = location
else:
# If the location is ``None``, then the grid is solved.
Expand Down