GeezerVison is showing github.com — Simplify sudoku.is_completed() using builtin all() (#1608) · SharathKumarGithub/Python@26b0803
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: backtracking/sudoku.py +16-20Lines changed: 16 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """ | 2 | | -
| 3 | 2 | Given a partially filled 9×9 2D array, the objective is to fill a 9×9 | 4 | 3 | square grid with digits numbered 1 to 9, so that every row, column, and | 5 | 4 | and each of the nine 3×3 sub-grids contains all of the digits. | | 9 | 8 | function on the next column to see if it returns True. if yes, we | 10 | 9 | have solved the puzzle. else, we backtrack and place another number | 11 | 10 | in that cell and repeat this process. | 12 | | -
| 13 | 11 | """ | 14 | | - | 15 | 12 | # assigning initial values to the grid | 16 | 13 | initial_grid = [ | 17 | 14 | [3, 0, 6, 5, 0, 8, 4, 0, 0], | | 24 | 21 | [0, 0, 0, 0, 0, 0, 0, 7, 4], | 25 | 22 | [0, 0, 5, 2, 0, 6, 3, 0, 0], | 26 | 23 | ] | | 24 | + | 27 | 25 | # a grid with no solution | 28 | 26 | no_solution = [ | 29 | 27 | [5, 0, 6, 5, 0, 8, 4, 0, 3], | @@ -44,9 +42,7 @@ def is_safe(grid, row, column, n): | 44 | 42 | column, and the 3x3 subgrids contain the digit 'n'. | 45 | 43 | It returns False if it is not 'safe' (a duplicate digit | 46 | 44 | is found) else returns True if it is 'safe' | 47 | | -
| 48 | 45 | """ | 49 | | - | 50 | 46 | for i in range(9): | 51 | 47 | if grid[row][i] == n or grid[i][column] == n: | 52 | 48 | return False | @@ -62,26 +58,29 @@ def is_safe(grid, row, column, n): | 62 | 58 | def is_completed(grid): | 63 | 59 | """ | 64 | 60 | 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 | 68 | 75 | """ | 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) | 76 | 77 |
| 77 | 78 |
| 78 | 79 | def find_empty_location(grid): | 79 | 80 | """ | 80 | 81 | This function finds an empty location so that we can assign a number | 81 | 82 | for that particular row and column. | 82 | | -
| 83 | 83 | """ | 84 | | - | 85 | 84 | for i in range(9): | 86 | 85 | for j in range(9): | 87 | 86 | if grid[i][j] == 0: | @@ -129,17 +128,14 @@ def print_solution(grid): | 129 | 128 | """ | 130 | 129 | A function to print the solution in the form | 131 | 130 | of a 9x9 grid | 132 | | -
| 133 | 131 | """ | 134 | | - | 135 | 132 | for row in grid: | 136 | 133 | for cell in row: | 137 | 134 | print(cell, end=" ") | 138 | 135 | print() | 139 | 136 |
| 140 | 137 |
| 141 | 138 | if __name__ == "__main__": | 142 | | - | 143 | 139 | # make a copy of grid so that you can compare with the unmodified grid | 144 | 140 | for grid in (initial_grid, no_solution): | 145 | 141 | grid = list(map(list, grid)) | |
Collapse file Copy file name to clipboardExpand all lines: divide_and_conquer/convex_hull.py +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,7 @@ class Point: | 28 | 28 | Examples | 29 | 29 | -------- | 30 | 30 | >>> Point(1, 2) | 31 | | - (1, 2) | | 31 | + (1.0, 2.0) | 32 | 32 | >>> Point("1", "2") | 33 | 33 | (1.0, 2.0) | 34 | 34 | >>> Point(1, 2) > Point(0, 1) | @@ -41,7 +41,7 @@ class Point: | 41 | 41 | Traceback (most recent call last): | 42 | 42 | ... | 43 | 43 | ValueError: x and y must be both numeric types but got <class 'str'>, <class 'str'> instead | 44 | | - """ | | 44 | + """ | 45 | 45 |
| 46 | 46 | def __init__(self, x, y): | 47 | 47 | if not (isinstance(x, Number) and isinstance(y, Number)): | @@ -200,8 +200,7 @@ def _validate_input(points): | 200 | 200 | ) | 201 | 201 | elif not hasattr(points, "__iter__"): | 202 | 202 | 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}" | 205 | 204 | ) | 206 | 205 | except TypeError as e: | 207 | 206 | print("Expecting an iterable of type Point, list or tuple.") | |
Collapse file Copy file name to clipboardExpand all lines: graphs/dijkstra_algorithm.py +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -104,9 +104,7 @@ def show_graph(self): | 104 | 104 | # u -> v(w) | 105 | 105 | for u in self.adjList: | 106 | 106 | 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]), | 110 | 108 | ) | 111 | 109 |
| 112 | 110 | def dijkstra(self, src): | |
Collapse file Copy file name to clipboardExpand all lines: machine_learning/sequential_minimum_optimization.py +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -499,9 +499,7 @@ def test_cancel_data(): | 499 | 499 | for i in range(test_tags.shape[0]): | 500 | 500 | if test_tags[i] == predict[i]: | 501 | 501 | 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}") | 505 | 503 | print(f"Rough Accuracy: {score / test_tags.shape[0]}") | 506 | 504 |
| 507 | 505 |
| |
|