This program is the first of two programming problems on the final exam; it will test your use of functions, sets, lists, and nested loops, as well as proper documentation and style.
You must complete this exercise on your own, without the help of others. You may use the textbooks, your notes, your previous assignments, the notes and examples on our course site and the Python library documentation. Use of any other source, such as Google, is not permitted. You may not work with, discuss, or in any way collaborate with anyone else. You may only ask the tutors or ASI for help with hardware problems or difficulties submitting your program.
You are encouraged to reuse useful code from your assignments or our class examples. Partial credit will be awarded, so if you can’t solve the whole problem, get as far as you can. If you are stuck, contact me. While I cannot help you solve the programming problem itself, I may be able to suggest general problem-solving strategies, help with conceptual difficulties about Python, and/or direct you to relevant examples from class.
The use of imported modules is not permitted.
For this programming problem you will implement functions that check the validity of square grids of integers.
Let’s define a Mudoku (a made-up simple variant of a Sudoku) to be a N x N grid in which each row and each column contain the numbers 1 through N. (In case you’re familiar with regular sudokus, note that here we just have rows and columns; there is no notion of sub-blocks.)
For example:
A. A valid 3 x 3 mudoku:
3 2 1
2 1 3
1 3 2
B. A valid 4 x 4 mudoku:
1 2 3 4
2 3 4 1
4 1 2 3
3 4 1 2
C. Not a mudoku because the grid contains a number not in the range 1-3:
1 2 3
2 3 4
3 1 2
D. Not a mudoku because the grid contains the number 3 twice in column 3 and in row 4:
2 1 3 4
3 2 4 1
1 4 3 2
4 3 1 3
In Python, we can represent such grids as lists of lists. For instance, the above examples could be stored as
A = [[3, 2, 1], [2, 1, 3], [1, 3, 2]]
B = [[1, 2, 3, 4], [2, 3, 4, 1], [4, 1, 2, 3], [3, 4, 1, 2]]
C = [[1, 2, 3], [2, 3, 4], [3, 1, 2]]
D = [[2, 1, 3, 4], [3, 2, 4, 1], [1, 4, 3, 2], [4, 3, 1, 3]]
print_mudoku
to print a N x N gridWrite a function print_mudoku
that takes a list representing an N x N
mudoku and prints it as a grid.
You may assume 2 <= N <= 9. Sample
runs using the variables defined above:
>>> print_mudoku(A)
3 2 1
2 1 3
1 3 2
>>> print_mudoku(B)
1 2 3 4
2 3 4 1
4 1 2 3
3 4 1 2
>>> print_mudoku(C)
1 2 3
2 3 4
3 1 2
>>> print_mudoku(D)
2 1 3 4
3 2 4 1
1 4 3 2
4 3 1 3
is_mudoku
to test if a grid is a MudokuWrite a function is_mudoku
that takes a list representing a mudoku
and returns True if it is a valid mudoku, and False otherwise.
Sample runs, given the variables define above:
>>> is_mudoku(A)
True
>>> is_mudoku(B)
True
>>> is_mudoku(C)
False
>>> is_mudoku(D)
False
>>> is_mudoku([[1, 2], [2, 1]])
True
>>> is_mudoku([[1, 2], [3, 4]])
False
reasons_not_mudoku
to report why a grid is not a MudokuWrite a function reasons_not_mudoku
that takes a list representing a
mudoku and prints the reasons why it’s not a valid mudoku. It should print
sentences of the following form, one for each violation:
Duplicate values in row X
Duplicate values in column Y
Illegal value Z
The order in which these lines are printed does not matter. If given a valid mudoku, the function should print nothing.
check_mudoku
to call your other functionsFinally, write a function check_mudoku
that takes a mudoku and uses your
previous functions to (1) print it, (2) print “Is a valid mudoku” or
“Is NOT a valid mudoku:”, and (3) print the reasons.
Sample runs:
>>> check_mudoku(A)
3 2 1
2 1 3
1 3 2
Is a valid mudoku
>>> check_mudoku(C)
1 2 3
2 3 4
3 1 2
Is NOT a valid mudoku:
Illegal value 4
>>> check_mudoku(D)
2 1 3 4
3 2 4 1
1 4 3 2
4 3 1 3
Is NOT a valid mudoku:
Duplicate values in column 3
Duplicate values in row 4
>>> check_mudoku([[1, 2], [1, 2]])
1 2
1 2
Is NOT a valid mudoku:
Duplicate values in column 1
Duplicate values in column 2
>>> check_mudoku([[1, 2], [3, 4]])
1 2
3 4
Is NOT a valid mudoku:
Illegal value 3
Illegal value 4
>>> check_mudoku([[7, 2], [4, 4]])
7 2
4 4
Is NOT a valid mudoku:
Duplicate values in row 2
Illegal value 4
Illegal value 7
Your program must implement the above four functions, each having a single parameter
grid
that is a list of N lists containing N numbers each, with 2 <= N <= 9.
print_mudoku
prints its parameter as a grid. The function
has no return value.
is_mudoku
is a boolean function that returns whether its parameter
is a valid mudoku. The function does not print anything.
reasons_not_mudoku
prints all reasons (as described above)
its parameter is not a valid
mudoku. If the grid is a valid mudoku the function prints nothing. The
function has no return value.
check_mudoku
calls the above functions to (1) print
its parameter, (2) print “Is a valid mudoku” or
“Is NOT a valid mudoku:”, and (3) print the reasons.
The function has no return value.
No main program is required; and when imported, your program should do nothing.
When you write a function, test it as a stand-alone function from the Python shell.
To avoid unwanted blank lines in what gets printed, recall these two approaches:
Use the optional keyword parameter
end
to specify what should be printed after each item (by default end
is the
newline character). See help(print)
. For example:
>>> for i in range(3):
... print(i, end=" ")
... print()
0 1 2
>>>
Or, build up a string, and then print that string:
>>> s = ''
>>> for i in range(3):
... s += str(i)
...
>>> print(s)
012
In either approach, you can use print()
or embed the newline character ‘\n’
wherever needed to get the cursor to the next line.
Make sure that your program is properly documented:
And use good style:
Submit your program via Gradescope. Your program file must be named fp_mudoku.py. You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.
Gradescope will import your file for testing so make sure that no code executes on import.
Feature | Points |
---|---|
print_mudoku defined with one parameter |
1 |
print_mudoku functionality |
5 |
is_mudoku defined with one parameter |
1 |
is_mudoku functionality |
5 |
reasons_not_mudoku defined with one parameter |
1 |
reasons_not_mudoku functionality |
5 |
check_mudoku defined with one parameter |
1 |
check_mudoku functionality |
4 |
Style | 4 |
Documentation | 3 |
Total | 30 |