Skip to content

Simplify sudoku.is_completed() using builtin all() #1608

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 13 commits into from
Dec 8, 2019
36 changes: 16 additions & 20 deletions backtracking/sudoku.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""

Given a partially filled 9×9 2D array, the objective is to fill a 9×9
square grid with digits numbered 1 to 9, so that every row, column, and
and each of the nine 3×3 sub-grids contains all of the digits.
Expand All @@ -9,9 +8,7 @@
function on the next column to see if it returns True. if yes, we
have solved the puzzle. else, we backtrack and place another number
in that cell and repeat this process.

"""

# assigning initial values to the grid
initial_grid = [
[3, 0, 6, 5, 0, 8, 4, 0, 0],
Expand All @@ -24,6 +21,7 @@
[0, 0, 0, 0, 0, 0, 0, 7, 4],
[0, 0, 5, 2, 0, 6, 3, 0, 0],
]

# a grid with no solution
no_solution = [
[5, 0, 6, 5, 0, 8, 4, 0, 3],
Expand All @@ -44,9 +42,7 @@ def is_safe(grid, row, column, n):
column, and the 3x3 subgrids contain the digit 'n'.
It returns False if it is not 'safe' (a duplicate digit
is found) else returns True if it is 'safe'

"""

for i in range(9):
if grid[row][i] == n or grid[i][column] == n:
return False
Expand All @@ -62,26 +58,29 @@ def is_safe(grid, row, column, n):
def is_completed(grid):
"""
This function checks if the puzzle is completed or not.
it is completed when all the cells are assigned with a number(not zero)
and There is no repeating number in any column, row or 3x3 subgrid.

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
"""

for row in grid:
for cell in row:
if cell == 0:
return False

return True
return all(all(cell != 0 for cell in row) for row in grid)


def find_empty_location(grid):
"""
This function finds an empty location so that we can assign a number
for that particular row and column.

"""

for i in range(9):
for j in range(9):
if grid[i][j] == 0:
Expand Down Expand Up @@ -129,17 +128,14 @@ def print_solution(grid):
"""
A function to print the solution in the form
of a 9x9 grid

"""

for row in grid:
for cell in row:
print(cell, end=" ")
print()


if __name__ == "__main__":

# make a copy of grid so that you can compare with the unmodified grid
for grid in (initial_grid, no_solution):
grid = list(map(list, grid))
Expand Down
7 changes: 3 additions & 4 deletions divide_and_conquer/convex_hull.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Point:
Examples
--------
>>> Point(1, 2)
(1, 2)
(1.0, 2.0)
>>> Point("1", "2")
(1.0, 2.0)
>>> Point(1, 2) > Point(0, 1)
Expand All @@ -41,7 +41,7 @@ class Point:
Traceback (most recent call last):
...
ValueError: x and y must be both numeric types but got <class 'str'>, <class 'str'> instead
"""
"""

def __init__(self, x, y):
if not (isinstance(x, Number) and isinstance(y, Number)):
Expand Down Expand Up @@ -200,8 +200,7 @@ def _validate_input(points):
)
elif not hasattr(points, "__iter__"):
raise ValueError(
"Expecting an iterable object "
f"but got an non-iterable type {points}"
"Expecting an iterable object " f"but got an non-iterable type {points}"
)
except TypeError as e:
print("Expecting an iterable of type Point, list or tuple.")
Expand Down
4 changes: 1 addition & 3 deletions graphs/dijkstra_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ def show_graph(self):
# u -> v(w)
for u in self.adjList:
print(
u,
"->",
" -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]),
u, "->", " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]),
)

def dijkstra(self, src):
Expand Down
4 changes: 1 addition & 3 deletions machine_learning/sequential_minimum_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,7 @@ def test_cancel_data():
for i in range(test_tags.shape[0]):
if test_tags[i] == predict[i]:
score += 1
print(
f"\r\nall: {test_num}\r\nright: {score}\r\nfalse: {test_num - score}"
)
print(f"\r\nall: {test_num}\r\nright: {score}\r\nfalse: {test_num - score}")
print(f"Rough Accuracy: {score / test_tags.shape[0]}")


Expand Down