Appendix

1. Fundamentals of Python

Activity 1.01: Building a Sudoku Solver

Solution

  1. First, we define the Solver class to store its input puzzle in its cells attribute, as follows:

    from copy import deepcopy

    class Solver:

        def __init__(self, input_path):

            # Read in the input file and initialize the puzzle

            with open(input_path, 'r') as f:

                lines = f.readlines()

            self.cells = [list(map(int, line.split(','))) \

                          for line in lines]

  2. The helper method that prints out the puzzle in a nice format can loop through the individual cells in the puzzle while inserting the separating characters '-' and '|' at the appropriate places:

        # Print out the initial puzzle or solution in a nice format.

        def display_cell(self): ...

Get The Statistics and Calculus with Python Workshop now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.