python programming

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

python programming

Post by answerhappygod »

python programming
Python Programming 1
Python Programming 1 (41.39 KiB) Viewed 43 times
We've covered an example program to check if a given Sudoku solution is valid, as attached to the end. Highlighted part of the program checks if a given element is unique in its 3x3 box. Please ONLY re-write the highlighted code to make it check if the element is unique on its main diagonal lines (shown in highlighted cells below). Note that you need to check two main diagonal lines for a given 9x9 Sudoku solution. # Check whether a solution is valid def is Valid(grid): for i in range(9): for j in range(9): if grid[j] < 1 or grid[j] > 9 \ or not isValidAt(i, j, grid): return false return True # The fixed cells are valid
# Check whether grid[j] is valid in the grid def is ValidAt(i, j, grid): # Check whether grid[j] is valid at the i's row for column in range(9): if column != j and grid[column] == grid[j]: return false # Check whether grid[j]) is valid at the j's column for row in range(9): if row != i and grid[row] == grid[j] return false # Check whether grid[j] is valid in the 3 by 3 box for row in range((i // 3) * 3, (i // 3) * 3 + 3): for col in range((j // 3) * 3, (j // 3) * 3+3): if row != i and col != j and \ grid[row][col] == grid[i][j] return false return True # The current value at grid[i][j] is valid
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply