在python中显示2D数独板

时间:2015-02-02 16:54:47

标签: python 2d sudoku

我试图在python中显示2D数独板:

0 0 3 |0 2 0 |6 0 0 
9 0 0 |3 0 5 |0 0 1 
0 0 1 |8 0 6 |4 0 0 
------+------+------
0 0 8 |1 0 2 |9 0 0 
7 0 0 |0 0 0 |0 0 8 
0 0 6 |7 0 8 |2 0 0 
------+------+------
0 0 2 |6 0 9 |5 0 0 
8 0 0 |2 0 3 |0 0 9 
0 0 5 |0 1 0 |3 0 0

我设法使用此代码显示没有分隔线的电路板:

rows = 'ABCDEFGHI'
cols = '123456789'

def display(values):
    for r in rows :
        for c in cols :
            print values[r+c],
    print 

值是字典{' A1'' 0',' A2':' 0',' A3&# 39;:' 3',' A4':' 0',' A5':' 2' ...我得到了这个输出:

0 0 3 0 2 0 6 0 0
9 0 0 3 0 5 0 0 1
0 0 1 8 0 6 4 0 0
0 0 8 1 0 2 9 0 0
7 0 0 0 0 0 0 0 8
0 0 6 7 0 8 2 0 0
0 0 2 6 0 9 5 0 0
8 0 0 2 0 3 0 0 9
0 0 5 0 1 0 3 0 0

任何帮助?

5 个答案:

答案 0 :(得分:1)

以下可能有效。但是我认为将字符串作为结果呈现的函数可能更有用(例如,将结果写入文本文件,而不需要太多的猴子修补)。

rows = 'ABCDEFGHI'
cols = '123456789'

def display(values):
    for i, r in enumerate(rows):
        if i in [3, 6]:
            print '------+-------+------'
        for j, c in enumerate(cols):
            if j in [3, 6]:
                print '|',
            print values[r + c],
        print

结果:

9 6 0 | 5 0 7 | 9 5 2
1 9 3 | 9 3 4 | 5 4 2
4 9 7 | 2 3 0 | 1 3 1
------+-------+------
3 0 1 | 6 7 3 | 9 8 3
2 4 5 | 7 8 7 | 8 0 8
0 1 4 | 9 3 9 | 3 9 6
------+-------+------
6 1 2 | 8 7 6 | 5 0 1
4 3 9 | 3 0 8 | 5 6 6
4 1 7 | 5 9 9 | 3 1 7

答案 1 :(得分:0)

sudoku="
0 0 3  0 2 0  6 0 0
9 0 0  3 0 5  0 0 1
0 0 1  8 0 6  4 0 0

0 0 8  1 0 2  9 0 0
7 0 0  0 0 0  0 0 8
0 0 6  7 0 8  2 0 0

0 0 2  6 0 9  5 0 0
8 0 0  2 0 3  0 0 9
0 0 5  0 1 0  3 0 0"

字符串中存在空格的地方,您可以用' - '

替换它
import re
re.sub(r'\s+', '-', sudoku)

不是您要找的?让我知道

答案 2 :(得分:0)

这应该可以解决问题:

rows = 'ABCDEFGHIJK'
cols = '123456789'

def display(values):
    for r in rows :
        if r == "D" or r == "H":
            print '------+-------+------'
        else: 
            for c in cols :
                if c%3 == 0 and c != 9:
                    print values[r+c] + "|"
                else:
                    print values[r+c]

答案 3 :(得分:0)

这是一种有点混乱的方法。如果您在行和列字符串中添加了一些其他标识符,那么您实际上会以可识别的形式获取列之间的内容:

# Use "x" as an identifier for a row or column where there should be a separator
rows = 'ABCxDEFxGHI'
cols = '123x456x789'

values = {'A1': '0', 'A2': '0', 'A3': '3'}
def display(values):
    for r in rows:
        for c in cols:
            if r == "x":
                if c == "x":
                    # Both row and column are the separator, show a plus
                    print "+",
                else:
                    # Only the row is the separator, show a dash
                    print "-"
            elif c == "x":
                    # Only the column is the separator, show a pipe
                print "|",
            else:
                # Not a separator, print the given cell (or ? if not found)
                print values.get(r+c, "?"),
        # Make sure there's a newline so we start a new row
        print ""

display(values)

另一种可能性是更聪明并将分隔符修改的单元格插入字典(即"xx": "+", "Ax": "|"),但这样做更多。您可以使用字典的get()方法自动填充其中一组(即默认返回管道或连字符)。

答案 4 :(得分:0)

python3解决方案:

values = {'A9': '1' , 'D8' : '9', 'A1': '7', ...}
sortedKeys = sorted(values)
for i in range(0, 9):
    if i != 0 and i % 3 == 0:
        print("- - - + - - - + - - -")
    for j in range(0, 9):
        if j != 0 and j % 3 == 0:
            print("|", end=' ')
        key = sortedKeys[i*9 + j]
        print(values[key], end=' ')
    print()