Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 390: 11.24

Answer

code

Work Step by Step

# 11.24 (Check Sudoku solution) Listing 11.7 checks whether a solution is valid by checking # whether every number is valid in the grid. Rewrite the program by checking # whether every row, column, and box has the numbers 1 to 9. def main(): # Read a solution grid = readASolution() if isValidGrid(grid): print("Valid solution") else: print("Invalid solution") # Read a solution from the console def readASolution(): print("Enter a Sudoku puzzle solution:") grid = [] for i in range(9): line = input().strip().split() grid.append([eval(x) for x in line]) return grid # Check whether the fixed cells of the grid are valid def isValidGrid(grid): # Check whether each row has numbers 1 through 9 for i in range(9): if not is1To9(grid[i]): # If grid[i] does not contain 1 through 9 return False # Check whether each column has numbers 1 through 9 for j in range(9): # Obtain a column in the one-dimensional array column = [] for i in range(9): column.append(grid[i][j]) if not is1To9(column): # If column does not contain 1 through 9 return False # Check whether each 3 by 3 box has numbers 1 through 9 for i in range(3): for j in range(3): # The starting element in a small 3 by 3 box k = 0 list = 9 * [0] # Get all numbers in the box to list for row in range(i * 3, i * 3 + 3): for column in range(j * 3, j * 3 + 3): list[k] = grid[row][column] k += 1 if not is1To9(list): # If the list does not contain 1 through 9 return False return True # The fixed cells are valid # Check whether the one-dimensional array contains 1 through 9 def is1To9(list): # Make a copy of the array temp = [x for x in list] # Sort the array temp.sort() # Check if list contains 1, 2, 3, through 9 return temp == [1, 2, 3, 4, 5, 6, 7, 8, 9] main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.