Search⌘ K
AI Features

Solution Review: Sudoku

Understand how to manage lists uniquely by exploring Python's slice and copy methods in the context of solving Sudoku challenges. Learn to avoid common errors related to object references and improve your Python programming skills.

We'll cover the following...

Let’s see the solution to the previous challenge.

Solution

C++
def generate_grid(n_rows, n_cols):
row = [0] * n_cols
grid = [row[:] for _ in range(n_rows)]
# 2 more possible ways of doing the same.
# grid = [row.copy() for _ in range(n_rows)]
# grid = [[0] * n_cols for _ in range(n_rows)]
return grid
def assign(grid, row, col, value):
grid[row][col] = value
return grid
rows, cols = 9, 9
sudoku = generate_grid(rows, cols)
print(assign(sudoku, 1, 0, 9))

...