Skip to content

Commit 26b0803

Browse files
authored
Simplify sudoku.is_completed() using builtin all() (TheAlgorithms#1608)
* Simplify sudoku.is_completed() using builtin all() Simplify __sudoku.is_completed()__ using Python builtin function [__all()__](https://docs.python.org/3/library/functions.html#all). * fixup! Format Python code with psf/black push * Update sudoku.py * fixup! Format Python code with psf/black push * Old style exception -> new style for Python 3 * updating DIRECTORY.md * Update convex_hull.py * fixup! Format Python code with psf/black push * e.args[0] = "msg" * ValueError: could not convert string to float: 'pi' * Update convex_hull.py * fixup! Format Python code with psf/black push
1 parent 9eb50cc commit 26b0803

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

backtracking/sudoku.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""
2-
32
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
43
square grid with digits numbered 1 to 9, so that every row, column, and
54
and each of the nine 3×3 sub-grids contains all of the digits.
@@ -9,9 +8,7 @@
98
function on the next column to see if it returns True. if yes, we
109
have solved the puzzle. else, we backtrack and place another number
1110
in that cell and repeat this process.
12-
1311
"""
14-
1512
# assigning initial values to the grid
1613
initial_grid = [
1714
[3, 0, 6, 5, 0, 8, 4, 0, 0],
@@ -24,6 +21,7 @@
2421
[0, 0, 0, 0, 0, 0, 0, 7, 4],
2522
[0, 0, 5, 2, 0, 6, 3, 0, 0],
2623
]
24+
2725
# a grid with no solution
2826
no_solution = [
2927
[5, 0, 6, 5, 0, 8, 4, 0, 3],
@@ -44,9 +42,7 @@ def is_safe(grid, row, column, n):
4442
column, and the 3x3 subgrids contain the digit 'n'.
4543
It returns False if it is not 'safe' (a duplicate digit
4644
is found) else returns True if it is 'safe'
47-
4845
"""
49-
5046
for i in range(9):
5147
if grid[row][i] == n or grid[i][column] == n:
5248
return False
@@ -62,26 +58,29 @@ def is_safe(grid, row, column, n):
6258
def is_completed(grid):
6359
"""
6460
This function checks if the puzzle is completed or not.
65-
it is completed when all the cells are assigned with a number(not zero)
66-
and There is no repeating number in any column, row or 3x3 subgrid.
67-
61+
it is completed when all the cells are assigned with a non-zero number.
62+
63+
>>> is_completed([[0]])
64+
False
65+
>>> is_completed([[1]])
66+
True
67+
>>> is_completed([[1, 2], [0, 4]])
68+
False
69+
>>> is_completed([[1, 2], [3, 4]])
70+
True
71+
>>> is_completed(initial_grid)
72+
False
73+
>>> is_completed(no_solution)
74+
False
6875
"""
69-
70-
for row in grid:
71-
for cell in row:
72-
if cell == 0:
73-
return False
74-
75-
return True
76+
return all(all(cell != 0 for cell in row) for row in grid)
7677

7778

7879
def find_empty_location(grid):
7980
"""
8081
This function finds an empty location so that we can assign a number
8182
for that particular row and column.
82-
8383
"""
84-
8584
for i in range(9):
8685
for j in range(9):
8786
if grid[i][j] == 0:
@@ -129,17 +128,14 @@ def print_solution(grid):
129128
"""
130129
A function to print the solution in the form
131130
of a 9x9 grid
132-
133131
"""
134-
135132
for row in grid:
136133
for cell in row:
137134
print(cell, end=" ")
138135
print()
139136

140137

141138
if __name__ == "__main__":
142-
143139
# make a copy of grid so that you can compare with the unmodified grid
144140
for grid in (initial_grid, no_solution):
145141
grid = list(map(list, grid))

divide_and_conquer/convex_hull.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Point:
2828
Examples
2929
--------
3030
>>> Point(1, 2)
31-
(1, 2)
31+
(1.0, 2.0)
3232
>>> Point("1", "2")
3333
(1.0, 2.0)
3434
>>> Point(1, 2) > Point(0, 1)
@@ -41,7 +41,7 @@ class Point:
4141
Traceback (most recent call last):
4242
...
4343
ValueError: x and y must be both numeric types but got <class 'str'>, <class 'str'> instead
44-
"""
44+
"""
4545

4646
def __init__(self, x, y):
4747
if not (isinstance(x, Number) and isinstance(y, Number)):
@@ -200,8 +200,7 @@ def _validate_input(points):
200200
)
201201
elif not hasattr(points, "__iter__"):
202202
raise ValueError(
203-
"Expecting an iterable object "
204-
f"but got an non-iterable type {points}"
203+
"Expecting an iterable object " f"but got an non-iterable type {points}"
205204
)
206205
except TypeError as e:
207206
print("Expecting an iterable of type Point, list or tuple.")

graphs/dijkstra_algorithm.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ def show_graph(self):
104104
# u -> v(w)
105105
for u in self.adjList:
106106
print(
107-
u,
108-
"->",
109-
" -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]),
107+
u, "->", " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]),
110108
)
111109

112110
def dijkstra(self, src):

machine_learning/sequential_minimum_optimization.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,7 @@ def test_cancel_data():
499499
for i in range(test_tags.shape[0]):
500500
if test_tags[i] == predict[i]:
501501
score += 1
502-
print(
503-
f"\r\nall: {test_num}\r\nright: {score}\r\nfalse: {test_num - score}"
504-
)
502+
print(f"\r\nall: {test_num}\r\nright: {score}\r\nfalse: {test_num - score}")
505503
print(f"Rough Accuracy: {score / test_tags.shape[0]}")
506504

507505

0 commit comments

Comments
 (0)