1
+ from typing import List , Tuple , Union
2
+
3
+ Matrix = List [List [int ]]
4
+
1
5
"""
2
6
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
3
7
square grid with digits numbered 1 to 9, so that every row, column, and
36
40
]
37
41
38
42
39
- def is_safe (grid , row , column , n ) :
43
+ def is_safe (grid : Matrix , row : int , column : int , n : int ) -> bool :
40
44
"""
41
45
This function checks the grid to see if each row,
42
46
column, and the 3x3 subgrids contain the digit 'n'.
@@ -55,7 +59,7 @@ def is_safe(grid, row, column, n):
55
59
return True
56
60
57
61
58
- def is_completed (grid ) :
62
+ def is_completed (grid : Matrix ) -> bool :
59
63
"""
60
64
This function checks if the puzzle is completed or not.
61
65
it is completed when all the cells are assigned with a non-zero number.
@@ -76,7 +80,7 @@ def is_completed(grid):
76
80
return all (all (cell != 0 for cell in row ) for row in grid )
77
81
78
82
79
- def find_empty_location (grid ) :
83
+ def find_empty_location (grid : Matrix ) -> Tuple [ int , int ] :
80
84
"""
81
85
This function finds an empty location so that we can assign a number
82
86
for that particular row and column.
@@ -87,7 +91,7 @@ def find_empty_location(grid):
87
91
return i , j
88
92
89
93
90
- def sudoku (grid ) :
94
+ def sudoku (grid : Matrix ) -> Union [ Matrix , bool ] :
91
95
"""
92
96
Takes a partially filled-in grid and attempts to assign values to
93
97
all unassigned locations in such a way to meet the requirements
@@ -124,7 +128,7 @@ def sudoku(grid):
124
128
return False
125
129
126
130
127
- def print_solution (grid ) :
131
+ def print_solution (grid : Matrix ) -> None :
128
132
"""
129
133
A function to print the solution in the form
130
134
of a 9x9 grid
0 commit comments