A Sudoku puzzle contains 81 "cells" of numbers in a 9 x 9 grid. One helpful approach to solving a Sudoku puzzle is to make a class that represents each cell in the puzzle along with three different formats of the Sudoku puzzle representing each region type:
Keep in mind that the definition for a cell's region is as follows:
Create a class Cell with the following attributes:
val: The value of the cell.rLocation: The location of the cell in rowData with the format (r0, r1).The class should have the following methods:
convIndices(): convert the row indices into column and box index formats.getRegions(regionData): get the cell's region.Create a function getRegionData(puzzle):
Cell instances in each nested list.The Sudoku puzzle will be formatted in a 9 x 9 list where each nested list is a row in the puzzle:
puzzle = [
[0, 0, 0, 0, 0, 0, 9, 0, 6],
[3, 0, 2, 0, 6, 0, 0, 0, 0],
[8, 0, 0, 7, 0, 3, 0, 0, 5],
[0, 0, 3, 9, 0, 6, 0, 0, 0],
[6, 0, 0, 0, 5, 0, 0, 0, 0],
[0, 0, 4, 8, 0, 1, 0, 0, 0],
[7, 0, 0, 4, 0, 8, 0, 0, 2],
[5, 0, 8, 0, 9, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 0, 1]
]
Note: Zeros represent unsolved cells.
This function should return a list containing the three lists representing different region types of the puzzle. For these purposes, these lists will be referred to as:
rowData: same format as puzzle.colData: 2D list of columns.boxData: 2D list of boxes ordered as shown below.| 0 | 1 | 2 |
|---|---|---|
| 3 | 4 | 5 |
| 6 | 7 | 8 |
Note: Each box contains another 3 x 3 box ordered like its container. The numbers shown above represent indices that correspond to the following list structure:
[[0], ... , [8]] where each nested list is [0, ... , 8]This method should be called on initialization and create two new attributes:
cLocation: the location of the cell in colData with the format (c0, c1)bLocation: the location of the cell in boxData with the format (b0, b1)Hint: rowData[r0][r1] = colData[c0][c1] = boxData[b0][b1]
cell = Cell(_, (1, 2))
cell.rLocation
(1, 2)
cell.cLocation
(2, 1)
cell.bLocation
(0, 5)
This method should take in regionData and create one new attribute:
regions: a 2D list containing the row, column, and box region for the cell.Note: Each cell's region should contain Cell instances excluding the cell.
regionData = getRegionData(puzzle)
cell.getRegions(regionData)
cell.regions
[[row region], [col region], [box region]]
N/A