Question
Hello I need some help solving a 2 part task, I created the first task but need help with the second task. I have to
Hello I need some help solving a 2 part task, I created the first task but need help with the second task.
I have to solve a task with the help from the code i created.
the code below returns true or false if insertion is valid or not.
createfunction 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 nn sudoku grid; each item in the inner list is either an integer (example 13), 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.
the code for it is this:
Im having trouble creating the grids_augmented_in_row function
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
def valid_grid(grid, num, r, c):
for i in range(len(grid)):
if grid[i][c]==num:
return False
for i in range(len(grid[0])):
if grid[r][i]==num:
return False
lst=subgrid_values(grid,r,c)
for i in lst:
if i==num:
return False
else:
return True
grid = [ [1,'x','x','x'],
['x','x','x','x'],
['x','x',1,'x'],
['x','x','x','x'] ]
print(valid_grid(grid, 1, 1, 3)) #returns true
print(valid_grid(grid, 1, 0, 3)) #returns false
print(valid_grid(grid, 1, 1, 2)) #returns false
print(valid_grid(grid, 2, 0, 1)) #returns false
So using the code above how can i do the next task?
this is what i need help with:
create function grids_augmented_in_row(grid,num,r) that returns the complete list of valid augmented grids, where each grid contains num in row r.
Input: a nested list grid, that represents a valid nn sudoku grid; each item in the inner list is either an integer (example 27), or the string 'x'; a positive integer num, where 0 < num n; and a non-negative integer r, where 0 r < n.
Output: a nested list containing all augmented sudoku grids such that each grid is valid, and each grid contains num in row r. If num is in row r in the original grid, return a list containing the original grid. If there is no way to augment the given grid to create valid grid where num is in row r, return an empty list.
Remember that you may import deepcopy from copy.
Examples
lite_grid = [ [1,'x','x','x'], ['x','x','x','x'], ['x','x','x','x'], ['x',2,'x','x'] ]
full_grid = [ [2,'x','x','x'], ['x',3,2,4], ['x','x',4,2],[1,2,3,'x'] ]
grid_A = [ ['x','x',1,'x'], [4,'x','x','x'], ['x','x','x',2], ['x',3,'x','x'] ]
a) Calling enter_number_in_row(lite grid,1,0) returns:
[
#note there is already a 1 in row 0, so returns list containing original grid [ [1,'x','x','x'], ['x','x','x','x'], ['x','x','x','x'], ['x',2,'x','x'] ]
]
b) Calling enter_number_in_row(lite grid,1,1) returns:
[
[ [1,'x','x','x'], ['x','x',1,'x'], #note there is now a 1 in row 1 ['x','x','x','x'], ['x',2,'x','x'] ],
[ [1,'x','x','x'], ['x','x','x',1], #note there is now a 1 in row 1 ['x','x','x','x'], ['x',2,'x','x'] ]
]
c) Calling enter_number_in_row(full grid,1,1) returns [], because there is no valid way to insert a 1 in row 1 of full grid.
d) Calling enter_number(grid A,1,2) returns:
[
[ ['x','x',1,'x'], [4,'x','x','x'], [1,'x','x',2], #note there is now a 1 in row 2 ['x',3,'x','x'] ] ,
[ ['x','x',1,'x'], [4,'x','x','x'], ['x',1,'x',2], #note there is now a 1 in row 2 ['x',3,'x','x'] ]
]
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