Back to Freecodecamp

Step 27

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

latest1.3 KB
Original Source

--description--

It's time to test the valid_in_row method. Call valid_in_row on gameboard. Pass it 0 and 8 as the arguments and print the result.

Again, note how the method is defined with three parameters, yet it is called with only two arguments because self is automatically passed as the object on which the method is called.

--hints--

You should print the result of calling valid_in_row(0, 8) on gameboard.

js
({ test: () => assert(runPython(`_Node(_code).has_call("print(gameboard.valid_in_row(0, 8))")`)) })

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

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)
--fcc-editable-region--

--fcc-editable-region--