View the step-by-step solution to:

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 solve a task with the help from the code i created.

the code below returns true or false if insertion is valid or not. 


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×n 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 n×n 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'] ]

]

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes