Python exercise:
Please provide screen shoot of the code:
Sudoku writing a program:
1. First step is to get a data representation of the Sudoku grid
that we can work with. Here we are going to use a 2 dimensional
list. Zeros represent empty cells.
Write a short script that assigns the exact list from above to a
variable and prints the list.
Hint: If you name your variable ”grid” or any other name with 4
letters you can nicely format the list in your editor with just 2
tabs, as it fits Python’s indentation system perfectly, where
usually a tab is as long a 4 empty spaces.
2. Printing the grid:
While the formatting in our editor may look nice, the output of
printing the list certainly does not. To properly solve a problem,
it is not only important that the internal representation is
correct, but also that we have a nice way of checking the current
state or view the final result.
1. Write a function called print grid(grid), that takes a
grid (2d list) and prints it to the terminal like displayed
below
2. Comment out the previous print(), and display the grid
with your new function
3. Solving sudoku (possible)
When we solve a sudoku by hand we always check if it is possible
to place a digit in a certain cell. For example, if we look at our
soduko we might think about placing a 1 in the 1st row & 3rd
column, but if we check the 3 rules of sudoku:
1. No duplicate digits in the same row
2. No duplicate digits in the same column
3. No duplicate digits in the respective 3x3 field
Then we notice that we cant place a 1 there, because there is
already a 1 in 1st row and also in the 3rd column.
Your task: Write a function called possible(row, col,
d), that returns True only if it is possible to place digit d in
the respective row, column and 3x3 field based on the current grid
state (refering to the grid variable) and False otherwise.
4. Solving sudoku (solve)
Now let us bring it all together with
the solve() function which will not take any arguments.
The
intuition of how solve() works goes as
follows: • First we search for an empty cell (=0)
• Then we go through the digits 1-9 and check if they
are possible in that cell
• If a digit is possible, we place it in that cell
Nice, we have one digit in place! To now place another digit, we
can just call solve() again, which will find another
empty cell, assign it a fitting digit and so on. But, what we
have to consider is that we place our digits without thinking about
any future complications. So what if no digits are possible in a
cell because we made some bad placements earlier? If we went
through all digits 1-9 and none fit, then we return. With this
we return to right after the last solve call that was
invoked. And right there we have to undo the previous digit
placement that let us into a dead end, assign a new digit (if
possible, otherwise return again) and keep trying.
Once a solve() call finds no empty cells we can
consider the Sudoku solved. In that case we want to display our
grid using print grid() and we are done.
Your task:
Implement solve() as described above. You will find
some very natural language-like pseu- docode below that you can use
as a guide for your implementation. Make sure you understand and
comment your code.
Call solve() to solve the sudoku grid from above.
What happens when we return again after printing the
Sudoku grid? Why does this happen?
Python exercise: Please provide screen shoot of the code: Sudoku writing a program: 1. First step is to get a data repre
-
- Posts: 43759
- Joined: Sat Aug 07, 2021 7:38 am