Skip to content

Commit 3324bbb

Browse files
ruppysuppygithub-actions
and
github-actions
authored
Added sudoku type hints [Hacktober Fest] (#3124)
* chore(sudoku): added type hints [HACKTOBER-FEST] * updating DIRECTORY.md * chore: added matrix type alias Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 02d3ab8 commit 3324bbb

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Diff for: backtracking/sudoku.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import List, Tuple, Union
2+
3+
Matrix = List[List[int]]
4+
15
"""
26
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
37
square grid with digits numbered 1 to 9, so that every row, column, and
@@ -36,7 +40,7 @@
3640
]
3741

3842

39-
def is_safe(grid, row, column, n):
43+
def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
4044
"""
4145
This function checks the grid to see if each row,
4246
column, and the 3x3 subgrids contain the digit 'n'.
@@ -55,7 +59,7 @@ def is_safe(grid, row, column, n):
5559
return True
5660

5761

58-
def is_completed(grid):
62+
def is_completed(grid: Matrix) -> bool:
5963
"""
6064
This function checks if the puzzle is completed or not.
6165
it is completed when all the cells are assigned with a non-zero number.
@@ -76,7 +80,7 @@ def is_completed(grid):
7680
return all(all(cell != 0 for cell in row) for row in grid)
7781

7882

79-
def find_empty_location(grid):
83+
def find_empty_location(grid: Matrix) -> Tuple[int, int]:
8084
"""
8185
This function finds an empty location so that we can assign a number
8286
for that particular row and column.
@@ -87,7 +91,7 @@ def find_empty_location(grid):
8791
return i, j
8892

8993

90-
def sudoku(grid):
94+
def sudoku(grid: Matrix) -> Union[Matrix, bool]:
9195
"""
9296
Takes a partially filled-in grid and attempts to assign values to
9397
all unassigned locations in such a way to meet the requirements
@@ -124,7 +128,7 @@ def sudoku(grid):
124128
return False
125129

126130

127-
def print_solution(grid):
131+
def print_solution(grid: Matrix) -> None:
128132
"""
129133
A function to print the solution in the form
130134
of a 9x9 grid

0 commit comments

Comments
 (0)