Back to Freecodecamp

Step 30

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

latest1.6 KB
Original Source

--description--

Next, you're going to create a method that checks if a number can be inserted in a specified column of the sudoku board by checking if the number is not already present in that column.

Within the Board class, create a method named valid_in_col and give it three parameters: self, col and num. Where col and num are the column index and the number to be checked, respectively.

--hints--

You should create a new method named valid_in_col within the Board class.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Board").has_function("valid_in_col")`)) })

Your valid_in_col method should have three parameters: self, col, and num, in this order.

js
({ test: () => assert(runPython(`_Node(_code).find_class("Board").find_function("valid_in_col").has_args("self, col, num")`)) })

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