We consider in this exercise the validation of a Sudoku grid. The Sudoku a mathematical game. You do not need to know this game to make this exercise. A solved Sudoku grid is represented on the right. It is a grid composed of 9 lines and 9 columns. A grid is said to be valid if it respects the following rules : • each line contains exactly once each value from 1 to 9 • each column contains exactly once each value from 1 to 9 • each block contains exactly once each value from 1 to 9. A block is a square of 3 × 3 cells. There are 9 blocks in the grid, they are delimited in the example grid with the bold lines. Q1) Create a class Sudoku containing a main() method. Declare a variable named grid corresponding to a sudoku grid. Your grid may correspond to the example which is valid, or not (for example an invalid grid filled with 1's). Q2) Write a static function named validLine() that has two parameters : a grid and a line index i. This function checks if the line i is valid (i.e. contains exactly once each value from 1 to 9) or not. It may be useful to write and use another function that counts the number of occurrences of a given value k in the line i. Q3) Write a static method allLinesValid() that takes a grid as parameter and checks if all the lines are valid. You may use the previous method, even if you did not write it.
时间: 2024-02-14 17:13:29 浏览: 28
Sure, I can help with that! Here's the implementation of the Sudoku class with the main() method, validLine() static method, and allLinesValid() static method:
```python
class Sudoku:
def main(self):
# Declare a variable named grid corresponding to a sudoku grid
grid = [
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]
]
# Check if the grid is valid
print(self.isValid(grid))
@staticmethod
def validLine(grid, i):
# Check if the line i is valid (i.e. contains exactly once each value from 1 to 9)
for j in range(1, 10):
if grid[i].count(j) != 1:
return False
return True
@staticmethod
def allLinesValid(grid):
# Check if all the lines are valid
for i in range(0, 9):
if not Sudoku.validLine(grid, i):
return False
return True
def isValid(self, grid):
# Check if the grid is valid (i.e. each line, column, and block contains exactly once each value from 1 to 9)
for i in range(0, 9):
# Check if the line i is valid
if not Sudoku.validLine(grid, i):
return False
# Check if the column i is valid
column = [grid[j][i] for j in range(0, 9)]
if not Sudoku.validLine(column, 0):
return False
# Check if the block i is valid
block = []
for j in range(0, 3):
for k in range(0, 3):
block.append(grid[(i // 3) * 3 + j][(i % 3) * 3 + k])
if not Sudoku.validLine(block, 0):
return False
return True
```
To test the implementation, you can create an instance of the Sudoku class and call the main() method:
```python
sudoku = Sudoku()
sudoku.main()
```
This will output True if the grid is valid, and False otherwise.