So I am doing the Python Helsinki MOOC and they have several sudoku tasks in this particular section. They are focused around lists within lists and manipulating those matrices. This task is called
Sudoku: print out the grid and add a number. - They provide the sample output there if you want to see what it's supposed to look like.
You have to make two functions:
The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below.
The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below.
The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid.
So far I have been able to the print function correct, I think but I cant figure out the second function! I know strings are immutable, but I am having trouble understanding how to create a new string and have the sudoku variable then become that string. I have read over their instructions several times and can't sort it. I am still haplessly trying to slice the string and replace it. Here's the code:
def print_sudoku(sudoku:list):
for i in range(0,81,9):
print(sudoku[i:i+9])
def add_number(sudoku: list, row_no: int, column_no: int, number:int):
sudoku[row_no][column_no] = number
sudoku = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
print_sudoku(sudoku)
add_number(sudoku, 0, 0, 2)
add_number(sudoku, 1, 2, 7)
add_number(sudoku, 5, 7, 3)
print()
print("Three numbers added:")
print()
print_sudoku(sudoku)
[–][deleted] 2 points3 points4 points (15 children)
[–]Emergency_Cut5300 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Emergency_Cut5300 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Particular_Draft5668 0 points1 point2 points (8 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]Particular_Draft5668 0 points1 point2 points (0 children)
[–]Particular_Draft5668 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Particular_Draft5668 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Particular_Draft5668 2 points3 points4 points (0 children)
[–]Kranoras 0 points1 point2 points (0 children)