اذهب إلى المحتوى
  • 0

مشكلة في كود بايثون

Mohamed Farahat

السؤال

مرحبا

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]

            row_str.join(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 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]

]

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.

What is the problem here I cannot see it, thank you

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

المشكلة في ميثود __str__  حيث لم تقم باستخدام دالة join لربط النص بشكل صحيح ولا تحدث القيمة board_str بشكل صحيح.

أولاً عليك تحويل الصف إلى سلسلة نصية مع وضع مسافة بين الأرقام، واستبدال الأصفار بالنجوم، ثم إضافة السلسلة النصية للصف إلى السلسلة النهائية مع إضافة سطر جديد n\

كالتالي:

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

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...