Bug in Learn Classes and Objects by building a sudoku solver

!!!BUG!!!
Hi, I’m a python newbie and I’m learning it on this website very first time.
Btw i found a bug while testing the sudoku solver.
Even though there are some error handling in the ori code, but when i try to replace the number of eighth row,first column in the puzzle from 0 to 1, it doesn’t lead it to print the message of unsolvable.


This is another test, changing sixth column, third row from 0 to 3, lead to massive duplicate problem against sudoku rules.

I dont understand why it happened but there is one thing in my mind which is we can add another function/method to test out the initial puzzle is valid or not in order to improve efficiency.

Hope that there is anyone or developer can tell me why, million thanks!

1 Like

what code are you talking about? you should share that

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

    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)
            board_str += '\n'
        return board_str

    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 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 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 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]
]
solve_sudoku(puzzle)

Hi, the ori code comes with method to check whether it is valid in row/col/square, but it doesnt really work sometimes as the example bug in the output picture that i provided.

Can you please link to the step/project that you’re working on?

Can you please link to the step/project that you’re working on?
[/quote]

Good day! This is the project that i working on.

Good day! this is the project link

Ok, I see what you mean.

In the final code, if you pass in an invalid puzzle (repeated number) it “solves” the puzzle even though the result has a repeated character. Good catch!

However, The point of the workshop is to learn specific Python lessons, not necessarily to create a perfect sudoku solver.

If you have an idea to improve the program to check the input puzzle though, you should definitely add that functionality on your own! You could host this on google collab.

If you want you could submit that later as an improvement to the curriculum, I’m not sure this will be considered a “bug” at this point though. If you are going to increase the length of his workshop I think you would need to make a good learning case for it.

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.