Question
Create function valid_entry(grid, num, r, c) that determines whether a particular value can be entered at a particular location in a valid grid, while maintaining
Create function valid_entry(grid, num, r, c) that determines whether a particular value can be entered
at a particular location in a valid grid, while maintaining validity.
Input: a nested list grid, that represents an n x n sudoku grid; each item in the inner list is either an integer, or the
string 'x'; a positive integer num, where 0 < num n; and two non-negative integers r and c that
represent the row and column that num will be inserted, where 0 r,c < n. You may assume grid[r][c]=='x'.
Output: a boolean True if the insertion is valid; otherwise False. For the insertion to be
valid, it must result in a grid that does not contain duplicate numbers in any row, any column, or any
subgrid.
To assist with implementation of the following tasks, you may use the following function subgrid values. This function takes a nn sudoku grid, a row coordinate, and a column coordinate, and returns a list containing the n values of the subgrid that the item at the coordinates belongs to.
def subgrid_values(grid, row, col):
val = []
#get dimension of inner box
n = int(len(grid)**(0.5))
#get starting row and starting col
r = (row//n)*n
c = (col//n)*n
for i in range(r, r+n):
for j in range(c, c+n):
val.append(grid[i][j])
return val
can only import deepcopy from copy
Examples
grid =[[1,'x','x','x'],
['x','x','x','x'],
['x','x',1,'x'],
['x','x','x','x'] ]
a) Calling valid_grid(grid, 1,1,3) returns True.
b) Calling valid_grid(grid, 1,0,3) returns False, because there would be two 1s in row 0.
c) Calling valid_grid(grid, 1,1,2) returns False, because there would be two 1s in column 2.
d) Calling valid_grid(grid, 1,3,3) returns False, because there would be two 1s in the bottom left 22 subgrid.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started