Python code to write one pure and one nonpure function Write
Python code to write one pure and one non-pure function:
Write a pure function named checkSudoku that will check a proposed Sudoku puzzle solution and return True if it\'s correct, False otherwise. The puzzle grid is represented using a twodimensional list with 9 rows and 9 columns, and is passed as a single argument to the checkSudoku function.
There are two basic approaches to checking the solution:
1) Verify that every row, column and sub-square contains all the digits 1-9 or…
2) Check every cell in the grid and verify that is unique among all the elements in its row, column and sub-square.
Hint: you can obtain the index [a][b] of the upper-left corner a 3x3 sub-square for any grid index [i][j] using the following: a = i//3*3, b = j//3*3
Write a separate unitTest procedure (non-pure function) that will call checkSudoku and display either \"Solution\" or \"Not a solution\" for the sets of values on the next page (at a minimum):
Notes: Test your program thoroughly on a variety of values other than those included in the examples above (that is, all error cases, average and special/extreme cases).
Below is an example puzzle and its solution: 5 3 467 89 1 2 67 21953 48 1 9 83 4 2 5 67 85 9761 4 23 4 2 6 8 5 37 9 |1 7 13 924 856 9615 37284 2 87 4 1 9 635 3 45 2 86179 19 5 3 1 6 8 4 8 3 6 5Solution
def checkAllElementspresent(arr):
for i in range(9):
if i + 1 not in arr:
return False
return True
def checkSudoku(mat):
for i in range(9):
temp = mat[i]
if checkAllElementspresent(temp) == False:
return False
for i in range(9):
temp = []
for j in range(9):
temp.append(mat[j][i])
if checkAllElementspresent(temp) == False:
return False
for i in range(0, 9, 3):
for j in range(0, 9, 3):
temp = []
for p in range(i, i + 3):
for q in range(j, j + 3):
temp.append(mat[p][q])
if checkAllElementspresent(temp) == False:
return False
return True
arr = [ [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]]
if checkSudoku(arr) == True:
print \'Solution\'
else:
print \'Not a solution\'

