Back to Freecodecamp

Step 31

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

latest1.5 KB
Original Source

--description--

You need to check if a given number is not equal to the number in the specified column of the current row.

For this, replace pass with a generator expression that iterates over the range from 0 to 8 (inclusive), and for each row, evaluates whether the number at the specified row and column col on the board is different from num.

--hints--

You should delete pass and create a generator expression (self.board[row][col] != num for row in range(9)).

js
({ test: () => assert(runPython(`_Node(_code).find_class("Board").find_function("valid_in_col").find_body().is_equivalent("(self.board[row][col] != num for row in range(9))")`)) })

--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

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