Learn Classes and Objects by Building a Sudoku Solver - Step 73

Tell us what’s happening:

I seem to have all the parts, I just can’t figure out how they fit together. Please be straight forward with me; a lot of commenters here give cryptic clues I have to divide the meaning of.

Your code so far

class Board:
    def __init__(self, board):
        self.board = board

# User Editable Region

    def __str__(self):
        board_str = ''
        for row in self.board:
            row_str = [str([i] for i in row)]

# User Editable Region

    def find_empty_cell(self):
        for row, contents in enumerate(self.board):
            try:
                col = contents.index(0)
                return row, col
            except ValueError:
                pass
        return None

    def valid_in_row(self, row, num):
        return num not in self.board[row]

    def valid_in_col(self, col, num):
        return all(self.board[row][col] != num for row in range(9))

    def valid_in_square(self, row, col, num):
        row_start = (row // 3) * 3
        col_start = (col // 3) * 3
        for row_no in range(row_start, row_start + 3):
            for col_no in range(col_start, col_start + 3):
                if self.board[row_no][col_no] == num:
                    return False
        return True

    def is_valid(self, empty, num):
        row, col = empty
        valid_in_row = self.valid_in_row(row, num)
        valid_in_col = self.valid_in_col(col, num)
        valid_in_square = self.valid_in_square(row, col, num)
        return all([valid_in_row, valid_in_col, valid_in_square])

    def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = guess
                if self.solver():
                    return True
                self.board[row][col] = 0
        return False

def solve_sudoku(board):
    gameboard = Board(board)
    print(f'Puzzle to solve:\n{gameboard}')
    if gameboard.solver():
        print(f'Solved puzzle:\n{gameboard}')
    else:
        print('The provided puzzle is unsolvable.')
    return gameboard

puzzle = [
  [0, 0, 2, 0, 0, 8, 0, 0, 0],
  [0, 0, 0, 0, 0, 3, 7, 6, 2],
  [4, 3, 0, 0, 0, 0, 8, 0, 0],
  [0, 5, 0, 0, 3, 0, 0, 9, 0],
  [0, 4, 0, 0, 0, 0, 0, 2, 6],
  [0, 0, 0, 4, 6, 7, 0, 0, 0],
  [0, 8, 6, 7, 0, 4, 0, 0, 0],
  [0, 0, 0, 5, 1, 9, 0, 0, 8],
  [1, 7, 0, 0, 0, 6, 0, 0, 5]
]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0

Challenge Information:

Learn Classes and Objects by Building a Sudoku Solver - Step 73

They are using the socratic method to try and help you get that ‘I get it now’ moment.

In other words they are trying to lead you to figuring it out.

1 Like

Inside the for loop, declare a variable row_str and assign it a list comprehension that iterates over row and turns each item i in row into a string. Use the str() function for that.

If you know how a for loop works you can get this. The list compression here is essentially a for loop inside two square brakets. The output of the list compression is an array (hence the the square brackets) with elements that come from or are output from the expression .
So here is the list comprehension syntax assigned to a variable.

variable_name = [expression for item in iterable]

Where:

  • Your expression in this case is the function and what you pass to the function
  • row is the iterable
  • item or i is each item in your iterable

do you think you put each i as argument of str, or maybe the whole list?

1 Like

*divine
row_str = [str(i) for i in row]