| 
 | 1 | +# In this exercise we will complete two more functions for the sudoku project from the previous section: print_sudoku and add_number.  | 
 | 2 | + | 
 | 3 | +# The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument.   | 
 | 4 | +# The function should print out the grid in the format specified in the examples below.  | 
 | 5 | + | 
 | 6 | +# The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid,   | 
 | 7 | +# two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments.   | 
 | 8 | +# The function should add the digit to the specified location in the grid.  | 
 | 9 | + | 
 | 10 | +# sudoku  = [  | 
 | 11 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 12 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 13 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 14 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 15 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 16 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 17 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 18 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0],  | 
 | 19 | +#     [0, 0, 0, 0, 0, 0, 0, 0, 0]  | 
 | 20 | +# ]  | 
 | 21 | + | 
 | 22 | +# print_sudoku(sudoku)  | 
 | 23 | +# add_number(sudoku, 0, 0, 2)  | 
 | 24 | +# add_number(sudoku, 1, 2, 7)  | 
 | 25 | +# add_number(sudoku, 5, 7, 3)  | 
 | 26 | +# print()  | 
 | 27 | +# print("Three numbers added:")  | 
 | 28 | +# print()  | 
 | 29 | +# print_sudoku(sudoku)  | 
 | 30 | + | 
 | 31 | +# _ _ _  _ _ _  _ _ _  | 
 | 32 | +# _ _ _  _ _ _  _ _ _  | 
 | 33 | +# _ _ _  _ _ _  _ _ _  | 
 | 34 | + | 
 | 35 | +# _ _ _  _ _ _  _ _ _  | 
 | 36 | +# _ _ _  _ _ _  _ _ _  | 
 | 37 | +# _ _ _  _ _ _  _ _ _  | 
 | 38 | + | 
 | 39 | +# _ _ _  _ _ _  _ _ _  | 
 | 40 | +# _ _ _  _ _ _  _ _ _  | 
 | 41 | +# _ _ _  _ _ _  _ _ _  | 
 | 42 | + | 
 | 43 | +# Three numbers added:  | 
 | 44 | + | 
 | 45 | +# 2 _ _  _ _ _  _ _ _  | 
 | 46 | +# _ _ 7  _ _ _  _ _ _  | 
 | 47 | +# _ _ _  _ _ _  _ _ _  | 
 | 48 | + | 
 | 49 | +# _ _ _  _ _ _  _ _ _  | 
 | 50 | +# _ _ _  _ _ _  _ _ _  | 
 | 51 | +# _ _ _  _ _ _  _ 3 _  | 
 | 52 | + | 
 | 53 | +# _ _ _  _ _ _  _ _ _  | 
 | 54 | +# _ _ _  _ _ _  _ _ _  | 
 | 55 | +# _ _ _  _ _ _  _ _ _  | 
 | 56 | + | 
 | 57 | +# Hint  | 
 | 58 | + | 
 | 59 | +# Remember it is possible to call the print function without causing a line change:  | 
 | 60 | + | 
 | 61 | +# print("characters ", end="")  | 
 | 62 | +# print("without carriage returns", end="")  | 
 | 63 | + | 
 | 64 | +# characters without carriage returns  | 
 | 65 | + | 
 | 66 | +# Sometimes you need just a new line, which a print statement without any argument will achieve:  | 
 | 67 | + | 
 | 68 | +# print()  | 
 | 69 | + | 
 | 70 | +def print_sudoku(sudoku: list):  | 
 | 71 | +	row_count = 1  | 
 | 72 | +	for row in sudoku:  | 
 | 73 | +		col_count = 1  | 
 | 74 | +		for item in row:  | 
 | 75 | +			if item == 0:  | 
 | 76 | +				print("_ ", end="")  | 
 | 77 | +			else:  | 
 | 78 | +				print(f"{item} ", end="")  | 
 | 79 | +			if col_count == 3 or col_count == 6:  | 
 | 80 | +				print(" ", end="")  | 
 | 81 | +			col_count += 1  | 
 | 82 | +		if row_count == 3 or row_count == 6:  | 
 | 83 | +			print()  | 
 | 84 | +		row_count += 1  | 
 | 85 | +		print()  | 
 | 86 | + | 
 | 87 | +def add_number(sudoku: list, row_no: int, column_no: int, number:int):  | 
 | 88 | +	sudoku[row_no][column_no] = number  | 
 | 89 | + | 
0 commit comments