WORKING WITH SUDUKO IN PYTHON 3: PLEASE ANSWER ALL PARTS TO THIS MULTISTEP PROBLEM AND INDICATE CORRECT INDEXING
Write a function 'subSquareCheck(x,y,val,s)' that, given the x and y position of the cell we are checking ('x' and 'y' respectively), a value to check 'val', and a sudoku puzzle 's', returns False if the given value has already been used in the indicated sub-square, and True if the number is still available.
HINT: Up to now we have been creating ranges to loop over within the loop statement itself, but this need not be the case! While it's certainly possible to generate the correct ranges algebraicly, you could also use if-statements!
ANOTHER HINT: In our sudoku format, the first index selects the row, which means it corresponds to the y-coordinate.
There are several strategies that may be used to solve a sudoku puzzle, but the simplest is the so-called “process of elimination.” Using this strategy, we examine a cell's environment to see how many numbers are possible. This should be reasonably simple, since the range of possible values is known, and we already have three functions which perform the three checks necessary.
If there is only one value that is allowable, then the value gets written into that cell. If there is more than one possibility, we leave the cell blank. If there are no possible values for this cell, then the information given in the puzzle contradicts the three rules of sudokus. In this case, we will raise a “Sudoku Invalid” exception.
Write a function 'elimination(x,y,s)' that, given a zero-indexed x and y position in the puzzle ('x' and 'y' respectively), and a sudoku puzzle s, returns the either a zero or “blank” if there is more than one possible answer, the answer if there is exactly one possible answer, and raises a “Sudoku Invalid” exception if there are no possible answers.
Now that we have a strategy to apply (elimination), we should teach the computer how to apply strategies to a sudoku puzzle. However, the elimination strategy is only one of a number of possible sudoku solving strategies. For more information on sudoku solving strategies, take a look at the following youtube video: https://www.youtube.com/watch?v=b123EURtu3I . With respect to this video, we have only implemented somewhat dubiously named “Naked Singles” strategy.
Write a function 'applyStrategy(strategy, s)' which, given a strategy (in this case, a function with the input/output properties described in Question 3), and a puzzle s, applies that strategy to each cell in the puzzle. Have the function return True if the strategy worked, and False if it didnt (see below). It will become important later on to know if a strategy worked. To this end, we need to keep track of whether there are any differences between the strategy's return results and what was already in the puzzle. To clarify, if, for a particular cell, the strategy returns what the cell already contained, there has been no change, and the strategy failed for that cell. We are interested in whether at least one of the cells were changed when we applied the strategy to the entire puzzle.
HINT: There is no need to run a strategy on a cell that is not “blank” (i.e., contains zero).
DOUBLE HINT: Given that s is a mutable data type, we can simply modify the contents of the cells using assignment statements.
1d) We must repeatedly apply the given list of strategies to the puzzle until they stop working or the puzzle is solved (remember to use our function from Question 1!) We will try to apply each strategy in the list of strategies once to the puzzle per execution of the loop. If all of them return False, the puzzle has not changed, and we break the loop. If the algorithm fails to solve the puzzle, raise a “Sudoku Unsolvable” exception. Otherwise, return the solved puzzle.