Back to Freecodecamp

Step 26

curriculum/challenges/english/blocks/learn-classes-and-objects-by-building-a-sudoku-solver/6606beade9200b49aaeecd94.md

latest1.4 KB
Original Source

--description--

If num is not in the row, the expression evaluates to True and it means the number is valid for insertion.

If num is in the row, the expression evaluates to False and insertion would violate the rules.

Prepend a return keyword to the expression inside the valid_in_row method body, so that the validity of the number can be checked.

--hints--

You should return num not in self.board[row].

js
({ test: () => assert(runPython(`_Node(_code).find_class("Board").find_function("valid_in_row").find_body().is_equivalent("return num not in self.board[row]")`)) })

--seed--

--seed-contents--

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

    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
--fcc-editable-region--
    def valid_in_row(self, row, num):
        num not in self.board[row]
--fcc-editable-region--
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]
]

gameboard = Board(puzzle)