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

Tell us what’s happening:

How can i solve it, i tried many ways it doesn’t work for the join. Could someone help me please?

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) if i else '*' for i in row]
            board_str += ''.join(row_str)
            

# 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/126.0.0.0 Safari/537.36

Challenge Information:

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

It doesn’t work either when i try to do it this way:
row_str = ’ '.join([str(i) if i else ‘*’ for i in row])

board_str += row_str

Even when i do this it is still not working:
row_str += ‘’.join([str(i) if i else ‘*’ for i in row])
board_str = row_str

Even when i try this it is still not working:
row_str = [str(i) if i else ‘’ for i in row]
row_str = ‘’.join([str(i) if i else '
’ for i in row])
board_str += row_str

Hi @Davelove

You should use .join() to join the items in row_str with a space and add the result to the current value of board_str .

Try adding a single space between the quote marks.

Happy coding

What is the best way to practice the new concepts i have learned in order to make sure i understand them? Any advice will be appreciated!

There is no one approach I can tell you, everyone’s learning style and process is different, and it also changes over time.

Here are a few suggestions:

  • Read articles from News:
    Python join() – How to Combine a List into a String in Python
  • Summarise what you learnt, with written notes. Writing things down slows down the brain, and lets you process new information better.
  • Regularly review what you have learnt in the past few days, weeks, months.
  • Before starting a new certificate, read all the information about the topics / modules / practice projects. Jot down in a few words what you will learn. As you complete one part, reread the next two or three parts, so you know beforehand what to expect.
  • Accept that coding is difficult for everyone, so don’t think you are the only one feeling that way.
  • Have good quality sleep. When you are sleeping, the brain processes and stores what you learnt during the day. If you cannot remember what you learnt, then you did not learn it.
  • Practice coding skills. You can search the internet for ideas on projects, or make something you can use.