Simplify sudoku.is_completed() using builtin all() (#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
This commit is contained in:
Christian Clauss 2019-12-08 22:42:17 +01:00 committed by GitHub
parent 9eb50cc223
commit 26b0803319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 29 deletions

View File

@ -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.
@ -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],
@ -24,6 +21,7 @@ initial_grid = [
[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],
@ -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
@ -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:
@ -129,9 +128,7 @@ 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=" ")
@ -139,7 +136,6 @@ def print_solution(grid):
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))

View File

@ -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)
@ -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)):
@ -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.")

View File

@ -104,9 +104,7 @@ class Graph:
# 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):

View File

@ -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]}")