From 67ea4233bf369ac8d800861ddc21ec46042f13e7 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Fri, 9 Oct 2020 20:50:14 +0530 Subject: [PATCH 1/3] chore(sudoku): added type hints [HACKTOBER-FEST] --- backtracking/sudoku.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index b3d38b4cc7c7..e88acc569354 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -1,3 +1,5 @@ +from typing import List, Tuple, Union + """ 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 @@ -36,7 +38,7 @@ ] -def is_safe(grid, row, column, n): +def is_safe(grid: List[List[int]], row: int, column: int, n: int) -> bool: """ This function checks the grid to see if each row, column, and the 3x3 subgrids contain the digit 'n'. @@ -55,7 +57,7 @@ def is_safe(grid, row, column, n): return True -def is_completed(grid): +def is_completed(grid: List[List[int]]) -> bool: """ This function checks if the puzzle is completed or not. it is completed when all the cells are assigned with a non-zero number. @@ -76,7 +78,7 @@ def is_completed(grid): return all(all(cell != 0 for cell in row) for row in grid) -def find_empty_location(grid): +def find_empty_location(grid: List[List[int]]) -> Tuple[int, int]: """ This function finds an empty location so that we can assign a number for that particular row and column. @@ -87,7 +89,7 @@ def find_empty_location(grid): return i, j -def sudoku(grid): +def sudoku(grid: List[List[int]]) -> Union[List[List[int]], bool]: """ Takes a partially filled-in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements @@ -124,7 +126,7 @@ def sudoku(grid): return False -def print_solution(grid): +def print_solution(grid: List[List[int]]) -> None: """ A function to print the solution in the form of a 9x9 grid From a3549cce23eb0822e6007c8e4ab315dacaaa8528 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 9 Oct 2020 15:25:45 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d3a378c3a2ee..80859356cca9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -473,6 +473,7 @@ * [Binary Exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py) * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py) * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) + * [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py) * [Euclidean Gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py) * [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py) * [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py) @@ -487,6 +488,7 @@ * [Lru Cache](https://github.com/TheAlgorithms/Python/blob/master/other/lru_cache.py) * [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.py) * [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/other/markov_chain.py) + * [Max Sum Sliding Window](https://github.com/TheAlgorithms/Python/blob/master/other/max_sum_sliding_window.py) * [Nested Brackets](https://github.com/TheAlgorithms/Python/blob/master/other/nested_brackets.py) * [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py) * [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py) @@ -529,7 +531,6 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol2.py) * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol3.py) * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol4.py) - * [Test Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/test_solutions.py) * Problem 07 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol1.py) * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol2.py) @@ -595,11 +596,11 @@ * Problem 26 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_26/sol1.py) * Problem 27 - * [Problem 27 Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_27/problem_27_sol1.py) + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_27/sol1.py) * Problem 28 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_28/sol1.py) * Problem 29 - * [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py) + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/sol1.py) * Problem 30 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/sol1.py) * Problem 31 @@ -656,6 +657,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py) * Problem 67 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py) + * Problem 71 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_71/sol1.py) * Problem 76 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py) * Problem 97 From 10010df8320dabb28ab237af75704ccb0a92a5e4 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose Date: Sat, 10 Oct 2020 11:50:55 +0530 Subject: [PATCH 3/3] chore: added matrix type alias --- backtracking/sudoku.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index e88acc569354..614bdb8530ac 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -1,5 +1,7 @@ from typing import List, Tuple, Union +Matrix = List[List[int]] + """ 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 @@ -38,7 +40,7 @@ ] -def is_safe(grid: List[List[int]], row: int, column: int, n: int) -> bool: +def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool: """ This function checks the grid to see if each row, column, and the 3x3 subgrids contain the digit 'n'. @@ -57,7 +59,7 @@ def is_safe(grid: List[List[int]], row: int, column: int, n: int) -> bool: return True -def is_completed(grid: List[List[int]]) -> bool: +def is_completed(grid: Matrix) -> bool: """ This function checks if the puzzle is completed or not. it is completed when all the cells are assigned with a non-zero number. @@ -78,7 +80,7 @@ def is_completed(grid: List[List[int]]) -> bool: return all(all(cell != 0 for cell in row) for row in grid) -def find_empty_location(grid: List[List[int]]) -> Tuple[int, int]: +def find_empty_location(grid: Matrix) -> Tuple[int, int]: """ This function finds an empty location so that we can assign a number for that particular row and column. @@ -89,7 +91,7 @@ def find_empty_location(grid: List[List[int]]) -> Tuple[int, int]: return i, j -def sudoku(grid: List[List[int]]) -> Union[List[List[int]], bool]: +def sudoku(grid: Matrix) -> Union[Matrix, bool]: """ Takes a partially filled-in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements @@ -126,7 +128,7 @@ def sudoku(grid: List[List[int]]) -> Union[List[List[int]], bool]: return False -def print_solution(grid: List[List[int]]) -> None: +def print_solution(grid: Matrix) -> None: """ A function to print the solution in the form of a 9x9 grid