Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions