Skip to content

Commit c824b90

Browse files
authored
Remove redundent function in Backtracking Sudoku (TheAlgorithms#4499)
* Remove redundent function After reviewing this code, I've noticed that the `is_completed` function is a redundant operation. Increasing the number of loops required for each step of the sudoku solver. This should remove n² operations where n is the width of the grid. * Update sudoku.py Remove additional newline
1 parent b743e44 commit c824b90

File tree

1 file changed

+1
-27
lines changed

1 file changed

+1
-27
lines changed

backtracking/sudoku.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,6 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
5959
return True
6060

6161

62-
def is_completed(grid: Matrix) -> bool:
63-
"""
64-
This function checks if the puzzle is completed or not.
65-
it is completed when all the cells are assigned with a non-zero number.
66-
67-
>>> is_completed([[0]])
68-
False
69-
>>> is_completed([[1]])
70-
True
71-
>>> is_completed([[1, 2], [0, 4]])
72-
False
73-
>>> is_completed([[1, 2], [3, 4]])
74-
True
75-
>>> is_completed(initial_grid)
76-
False
77-
>>> is_completed(no_solution)
78-
False
79-
"""
80-
return all(all(cell != 0 for cell in row) for row in grid)
81-
82-
8362
def find_empty_location(grid: Matrix) -> Optional[Tuple[int, int]]:
8463
"""
8564
This function finds an empty location so that we can assign a number
@@ -111,12 +90,7 @@ def sudoku(grid: Matrix) -> Optional[Matrix]:
11190
>>> sudoku(no_solution) is None
11291
True
11392
"""
114-
115-
if is_completed(grid):
116-
return grid
117-
118-
location = find_empty_location(grid)
119-
if location is not None:
93+
if location := find_empty_location(grid):
12094
row, column = location
12195
else:
12296
# If the location is ``None``, then the grid is solved.

0 commit comments

Comments
 (0)