在 python 中显示数独程序
Displaying sudoku program in python
我必须更新带有网格和边框的嵌套列表数独。我的输出没有按预期打印,我不确定如何修复它。这是我的代码,包含测试输入案例和预期输出与现在的输出。
下面的代码有以下内容:
- 我需要使用网格线和边框更新三个嵌套数独列表。
- 接下来是我的代码和三个数独板的预期输出。
- 最后,link 现在有我的输出
这是描述:
Prints a given board to the console in a way that aligns the content of columns and makes the subgrids visible.
Input : a Sudoku board (board) of size 4x4, 9x9, or 16x16
Effect: prints the board to the console
small = [[0, 0, 1, 0],
[4, 0, 0, 0],
[0, 0, 0, 2],
[0, 3, 0, 0]]
big = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
[4, 0, 0, 7, 8, 9, 0, 0, 0],
[7, 8, 0, 0, 0, 0, 0, 5, 6],
[0, 2, 0, 3, 6, 0, 8, 0, 0],
[0, 0, 5, 0, 0, 7, 0, 1, 0],
[8, 0, 0, 2, 0, 0, 0, 0, 5],
[0, 0, 1, 6, 4, 0, 9, 7, 0],
[0, 0, 0, 9, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 0, 0, 2]]
giant = [[0, 5, 0, 0, 0, 4, 0, 8, 0, 6, 0, 0, 0, 0, 9, 16],
[1, 0, 0, 0, 0, 0, 0, 13, 4, 0, 0, 7, 15, 0, 8, 0],
[13, 0, 0, 0, 0, 7, 3, 0, 0, 0, 0, 9, 5, 10, 0, 0],
[0, 11, 12, 15, 10, 0, 0, 0, 0, 0, 5, 0, 3, 4, 0, 13],
[15, 0, 1, 3, 0, 0, 7, 2, 0, 0, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 12, 0, 3, 0, 5, 0, 11, 0, 14, 0, 0, 0, 9],
[4, 7, 0, 0, 0, 0, 0, 0, 12, 0, 15, 16, 0, 0, 0, 0],
[0, 0, 0, 0, 14, 0, 15, 0, 6, 9, 0, 0, 0, 0, 12, 0],
[3, 0, 15, 4, 0, 13, 14, 0, 0, 0, 0, 1, 0, 0, 7, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0],
[11, 0, 16, 10, 0, 0, 0, 0, 0, 7, 0, 0, 0, 3, 5, 0],
[0, 0, 13, 0, 0, 0, 0, 0, 14, 0, 15, 16, 0, 9, 0, 1],
[9, 0, 2, 0, 0, 14, 0, 4, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 14, 0, 0, 0, 0, 0, 10, 9, 0, 3, 0, 0, 0, 1, 7],
[8, 0, 0, 0, 16, 0, 0, 1, 2, 14, 11, 4, 0, 0, 0, 3],
[0, 0, 0, 1, 0, 0, 5, 0, 0, 16, 0, 6, 0, 12, 0, 0]]
import math
def print_board(board):
board_size = len(board)
l = int(board_size + math.sqrt(board_size) + 1)
section = int(math.sqrt(board_size))
border = 0
i_index = 0
for i in range(0, l):
if (i == border):
print('-' * l)
border = border + section + 1
else:
inner_border = 0
j_index = 0
for j in range(0, l):
if (j == inner_border):
print('|', end='')
inner_border = inner_border + section + 1
else:
if (board[i_index][j_index] == 0):
print(' ', end='')
else:
if (board[i_index][j_index] < 10):
print(board[i_index][j_index], end='')
if (board[i_index][j_index] == 10):
print('A', end='')
if (board[i_index][j_index] == 11):
print('B', end='')
if (board[i_index][j_index] == 12):
print('C', end='')
if (board[i_index][j_index] == 13):
print('D', end='')
if (board[i_index][j_index] == 14):
print('E', end='')
if (board[i_index][j_index] == 15):
print('F', end='')
if (board[i_index][j_index] == 16):
print('G', end='')
j_index = j_index + 1
i_index = i_index + 1
print(print_board(small))
print(print_board(big))
print(print_board(giant))
预期输出:
-------
| |1 |
|4 | |
-------
| | 2|
| 3| |
-------
>>> print_board(big)
-------------
| | | |
|4 |789| |
|78 | | 56|
-------------
| 2 |36 |8 |
| 5| 7| 1 |
|8 |2 | 5|
-------------
| 1|64 |97 |
| |9 | |
| | 3 | 2|
-------------
>>> print_board(giant2)
---------------------
| 5 | 4 8| 6 | 9G|
|1 | D|4 7|F 8 |
|D | 73 | 9|5A |
| BCF|A | 5 |34 D|
---------------------
|F 13| 72| | 5 |
| C| 3 5| B E| 9|
|47 | |C FG| |
| |E F |69 | C |
---------------------
|3 F4| DE | 1| 78|
| | | 9A| |
|B GA| | 7 | 35 |
| D | |E GF| 9 1|
---------------------
|9 2 | E 4|8 | |
| E | A|9 3 | 17|
|8 |G 1|2EB4| 3|
| 1| 5 | G 6| C |
---------------------
我的输出:
My_output
您需要在每行之间打印一个换行符,最简单的方法是在您要创建新行时使用 print()
。
试试这个:
# ...
i_index = i_index + 1
print()
# ...
当你增加 i_index
参数时,你基本上会转到下一行,所以这也是你想要 print()
语句的地方
此外,由于您在函数 print_board()
中使用了 print
,例如您不需要使用 print(print_board(big))
,您应该只使用 print_board(big)
。这就是为什么您会在每块板之间看到 None
(因为 print_board()
returns None)
我必须更新带有网格和边框的嵌套列表数独。我的输出没有按预期打印,我不确定如何修复它。这是我的代码,包含测试输入案例和预期输出与现在的输出。
下面的代码有以下内容:
- 我需要使用网格线和边框更新三个嵌套数独列表。
- 接下来是我的代码和三个数独板的预期输出。
- 最后,link 现在有我的输出
这是描述:
Prints a given board to the console in a way that aligns the content of columns and makes the subgrids visible.
Input : a Sudoku board (board) of size 4x4, 9x9, or 16x16
Effect: prints the board to the console
small = [[0, 0, 1, 0],
[4, 0, 0, 0],
[0, 0, 0, 2],
[0, 3, 0, 0]]
big = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
[4, 0, 0, 7, 8, 9, 0, 0, 0],
[7, 8, 0, 0, 0, 0, 0, 5, 6],
[0, 2, 0, 3, 6, 0, 8, 0, 0],
[0, 0, 5, 0, 0, 7, 0, 1, 0],
[8, 0, 0, 2, 0, 0, 0, 0, 5],
[0, 0, 1, 6, 4, 0, 9, 7, 0],
[0, 0, 0, 9, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 0, 0, 2]]
giant = [[0, 5, 0, 0, 0, 4, 0, 8, 0, 6, 0, 0, 0, 0, 9, 16],
[1, 0, 0, 0, 0, 0, 0, 13, 4, 0, 0, 7, 15, 0, 8, 0],
[13, 0, 0, 0, 0, 7, 3, 0, 0, 0, 0, 9, 5, 10, 0, 0],
[0, 11, 12, 15, 10, 0, 0, 0, 0, 0, 5, 0, 3, 4, 0, 13],
[15, 0, 1, 3, 0, 0, 7, 2, 0, 0, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 12, 0, 3, 0, 5, 0, 11, 0, 14, 0, 0, 0, 9],
[4, 7, 0, 0, 0, 0, 0, 0, 12, 0, 15, 16, 0, 0, 0, 0],
[0, 0, 0, 0, 14, 0, 15, 0, 6, 9, 0, 0, 0, 0, 12, 0],
[3, 0, 15, 4, 0, 13, 14, 0, 0, 0, 0, 1, 0, 0, 7, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0],
[11, 0, 16, 10, 0, 0, 0, 0, 0, 7, 0, 0, 0, 3, 5, 0],
[0, 0, 13, 0, 0, 0, 0, 0, 14, 0, 15, 16, 0, 9, 0, 1],
[9, 0, 2, 0, 0, 14, 0, 4, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 14, 0, 0, 0, 0, 0, 10, 9, 0, 3, 0, 0, 0, 1, 7],
[8, 0, 0, 0, 16, 0, 0, 1, 2, 14, 11, 4, 0, 0, 0, 3],
[0, 0, 0, 1, 0, 0, 5, 0, 0, 16, 0, 6, 0, 12, 0, 0]]
import math
def print_board(board):
board_size = len(board)
l = int(board_size + math.sqrt(board_size) + 1)
section = int(math.sqrt(board_size))
border = 0
i_index = 0
for i in range(0, l):
if (i == border):
print('-' * l)
border = border + section + 1
else:
inner_border = 0
j_index = 0
for j in range(0, l):
if (j == inner_border):
print('|', end='')
inner_border = inner_border + section + 1
else:
if (board[i_index][j_index] == 0):
print(' ', end='')
else:
if (board[i_index][j_index] < 10):
print(board[i_index][j_index], end='')
if (board[i_index][j_index] == 10):
print('A', end='')
if (board[i_index][j_index] == 11):
print('B', end='')
if (board[i_index][j_index] == 12):
print('C', end='')
if (board[i_index][j_index] == 13):
print('D', end='')
if (board[i_index][j_index] == 14):
print('E', end='')
if (board[i_index][j_index] == 15):
print('F', end='')
if (board[i_index][j_index] == 16):
print('G', end='')
j_index = j_index + 1
i_index = i_index + 1
print(print_board(small))
print(print_board(big))
print(print_board(giant))
预期输出:
-------
| |1 |
|4 | |
-------
| | 2|
| 3| |
-------
>>> print_board(big)
-------------
| | | |
|4 |789| |
|78 | | 56|
-------------
| 2 |36 |8 |
| 5| 7| 1 |
|8 |2 | 5|
-------------
| 1|64 |97 |
| |9 | |
| | 3 | 2|
-------------
>>> print_board(giant2)
---------------------
| 5 | 4 8| 6 | 9G|
|1 | D|4 7|F 8 |
|D | 73 | 9|5A |
| BCF|A | 5 |34 D|
---------------------
|F 13| 72| | 5 |
| C| 3 5| B E| 9|
|47 | |C FG| |
| |E F |69 | C |
---------------------
|3 F4| DE | 1| 78|
| | | 9A| |
|B GA| | 7 | 35 |
| D | |E GF| 9 1|
---------------------
|9 2 | E 4|8 | |
| E | A|9 3 | 17|
|8 |G 1|2EB4| 3|
| 1| 5 | G 6| C |
---------------------
我的输出:
My_output
您需要在每行之间打印一个换行符,最简单的方法是在您要创建新行时使用 print()
。
试试这个:
# ...
i_index = i_index + 1
print()
# ...
当你增加 i_index
参数时,你基本上会转到下一行,所以这也是你想要 print()
语句的地方
此外,由于您在函数 print_board()
中使用了 print
,例如您不需要使用 print(print_board(big))
,您应该只使用 print_board(big)
。这就是为什么您会在每块板之间看到 None
(因为 print_board()
returns None)