From 0d358b774edf60e999097851604c200b7bd89400 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 2 Dec 2019 19:22:26 +0100 Subject: [PATCH 01/12] 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). --- backtracking/sudoku.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index b33351fd4911..a53df06df66a 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -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 @@ [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 @@ -64,24 +60,15 @@ 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. - """ - - for row in grid: - for cell in row: - if cell == 0: - return False - - return True + return 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 +116,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 +124,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)) From 0b2245ae5c5e343cbb6752ce4ca7cf7ae97ad641 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 2 Dec 2019 18:23:31 +0000 Subject: [PATCH 02/12] fixup! Format Python code with psf/black push --- data_structures/data_structures/heap/heap_generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/data_structures/heap/heap_generic.py b/data_structures/data_structures/heap/heap_generic.py index c41a434542e7..fc17c1b1218e 100644 --- a/data_structures/data_structures/heap/heap_generic.py +++ b/data_structures/data_structures/heap/heap_generic.py @@ -30,7 +30,8 @@ def _swap(self, i, j): """Performs changes required for swapping two elements in the heap""" # First update the indexes of the items in index map. self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = ( - self.pos_map[self.arr[j][0]], self.pos_map[self.arr[i][0]] + self.pos_map[self.arr[j][0]], + self.pos_map[self.arr[i][0]], ) # Then swap the items in the list. self.arr[i], self.arr[j] = self.arr[j], self.arr[i] From 3c8b6b39cb48a2d2111d19422de0a1bf35f78645 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 3 Dec 2019 12:34:44 +0100 Subject: [PATCH 03/12] Update sudoku.py --- backtracking/sudoku.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index a53df06df66a..1fda8876ad9a 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -58,11 +58,22 @@ 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 """ - return all(cell != 0 for cell in row for row in grid) - + return all(all(cell != 0 for cell in row) for row in grid) def find_empty_location(grid): """ From 870f0d537e96d23d861f7ee0abc97409636e0514 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 3 Dec 2019 11:35:38 +0000 Subject: [PATCH 04/12] fixup! Format Python code with psf/black push --- backtracking/sudoku.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index 1fda8876ad9a..d864e2823a9b 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -75,6 +75,7 @@ def is_completed(grid): """ 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 From 887ddbdddb8952707cf6c89dc17abd53e444c072 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Dec 2019 18:36:44 +0100 Subject: [PATCH 05/12] Old style exception -> new style for Python 3 --- divide_and_conquer/convex_hull.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index bd88256ab01c..b9733df1b505 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -40,7 +40,7 @@ class Point: >>> Point("pi", "e") Traceback (most recent call last): ... - ValueError: x and y must be both numeric types but got , instead + ValueError("x and y must be both numeric types but got , instead") """ def __init__(self, x, y): From 29220637bbff1b4a600cc2ee05a1471265321226 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 8 Dec 2019 17:37:32 +0000 Subject: [PATCH 06/12] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a3db3636e34c..8fa83227bd72 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -252,6 +252,7 @@ * [Binomial Coefficient](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_coefficient.py) * [Ceil](https://github.com/TheAlgorithms/Python/blob/master/maths/ceil.py) * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) + * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) * [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py) * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py) * [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py) @@ -286,6 +287,7 @@ * [Prime Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py) * [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_numbers.py) * [Prime Sieve Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_sieve_eratosthenes.py) + * [Pythagoras](https://github.com/TheAlgorithms/Python/blob/master/maths/pythagoras.py) * [Qr Decomposition](https://github.com/TheAlgorithms/Python/blob/master/maths/qr_decomposition.py) * [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/quadratic_equations_complex_numbers.py) * [Radix2 Fft](https://github.com/TheAlgorithms/Python/blob/master/maths/radix2_fft.py) From e887328c1b4ebf21ca5da1c816d619082cf3d68e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Dec 2019 18:50:11 +0100 Subject: [PATCH 07/12] Update convex_hull.py --- divide_and_conquer/convex_hull.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index b9733df1b505..1c13d884cd3d 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -48,9 +48,9 @@ def __init__(self, x, y): try: x, y = float(x), float(y) except ValueError as e: - e.args = ( - "x and y must be both numeric types " - "but got {}, {} instead".format(type(x), type(y)), + raise ValueError( + "x and y must be both numeric types but got " + f"{type(x)}, {type(y)} instead" ) raise From ddc8a9a99362efa4af7c219b6203f71ed63539df Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 8 Dec 2019 18:09:39 +0000 Subject: [PATCH 08/12] fixup! Format Python code with psf/black push --- divide_and_conquer/convex_hull.py | 3 +-- graphs/dijkstra_algorithm.py | 4 +--- machine_learning/sequential_minimum_optimization.py | 4 +--- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 87f58fd63717..62ee472559c2 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -198,8 +198,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.") diff --git a/graphs/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py index 57733eb5106d..7dfb5fb9df48 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -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): diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index cb859602b29f..a98bd93f7a06 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -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]}") From 8d26c70661e027bcffb2f548a5826b16f268f2b8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Dec 2019 19:23:51 +0100 Subject: [PATCH 09/12] e.args[0] = "msg" --- divide_and_conquer/convex_hull.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 62ee472559c2..1e859e607a26 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -40,7 +40,7 @@ class Point: >>> Point("pi", "e") Traceback (most recent call last): ... - ValueError("x and y must be both numeric types but got , instead") + ValueError: "x and y must be both numeric types but got , instead" """ def __init__(self, x, y): @@ -48,10 +48,11 @@ def __init__(self, x, y): try: x, y = float(x), float(y) except ValueError as e: - raise ValueError( + e.args[0] = ( "x and y must be both numeric types but got " f"{type(x)}, {type(y)} instead" ) + raise self.x = x self.y = y From 4d9ffffbbf7d597120d42111aa995b7179dc02a5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Dec 2019 19:34:52 +0100 Subject: [PATCH 10/12] ValueError: could not convert string to float: 'pi' --- divide_and_conquer/convex_hull.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 1e859e607a26..272fc02c7c1a 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -1,5 +1,3 @@ -from numbers import Number - """ The convex hull problem is problem of finding all the vertices of convex polygon, P of a set of points in a plane such that all the points are either on the vertices of P or @@ -40,21 +38,12 @@ class Point: >>> Point("pi", "e") Traceback (most recent call last): ... - ValueError: "x and y must be both numeric types but got , instead" - """ + ValueError: could not convert string to float: 'pi' + """ def __init__(self, x, y): - if not (isinstance(x, Number) and isinstance(y, Number)): - try: - x, y = float(x), float(y) - except ValueError as e: - e.args[0] = ( - "x and y must be both numeric types but got " - f"{type(x)}, {type(y)} instead" - ) - raise - self.x = x - self.y = y + self.x = float(x) + self.y = float(y) def __eq__(self, other): return self.x == other.x and self.y == other.y From 98981567817917f8cc9bc5a08e33366c34afeb2c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Dec 2019 22:40:40 +0100 Subject: [PATCH 11/12] Update convex_hull.py --- divide_and_conquer/convex_hull.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 272fc02c7c1a..886498d2e095 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -1,3 +1,5 @@ +from numbers import Number + """ The convex hull problem is problem of finding all the vertices of convex polygon, P of a set of points in a plane such that all the points are either on the vertices of P or @@ -26,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) @@ -38,12 +40,22 @@ class Point: >>> Point("pi", "e") Traceback (most recent call last): ... - ValueError: could not convert string to float: 'pi' + ValueError: x and y must be both numeric types but got , instead """ def __init__(self, x, y): - self.x = float(x) - self.y = float(y) + if not (isinstance(x, Number) and isinstance(y, Number)): + try: + x, y = float(x), float(y) + except ValueError as e: + e.args = ( + "x and y must be both numeric types " + f"but got {type(x)}, {type(y)} instead" + ) + raise + + self.x = x + self.y = y def __eq__(self, other): return self.x == other.x and self.y == other.y @@ -188,7 +200,8 @@ 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.") From 5c579f45c297a89ddb7ff12db6c9a46b58a7008a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 8 Dec 2019 21:41:34 +0000 Subject: [PATCH 12/12] fixup! Format Python code with psf/black push --- divide_and_conquer/convex_hull.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py index 886498d2e095..f233e822c473 100644 --- a/divide_and_conquer/convex_hull.py +++ b/divide_and_conquer/convex_hull.py @@ -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.")