m4ge m4ge - 5 months ago 33
Python Question

Formating Sudoku Grids Python 3

So basically, I'm trying to use Python 3 to create a sudoku solver by implementing backtracking as a learning project, so I'm not going for perfect efficiency yet (I'm going to rewrite it afterwards to try to get it better, I have redundant variables and other random stuff used), just getting it to work. However while I'm working on it, debugging is getting to be a challenge, and I want to format the grid I'm using better as it goes along, however I don't really know how to do so. This is how I'm storing it right now:

grid = [
[4, 0, 0, 0, 0, 5, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 9, 8],
[3, 0, 0, 0, 8, 2, 4, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 8, 0],
[9, 0, 3, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 6, 7, 0],
[0, 5, 0, 0, 0, 9, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 9, 0, 7],
[6, 4, 0, 3, 0, 0, 0, 0, 0],
]


But I want to output it in this sort of format:

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


(including changing the 0s into blank spaces)

Right now I'm just using pprint.pprint to print it out, but it's getting challenging. As a total programming noob, how do I format the grid that way efficiently?

EDIT:
Could I know why I'm getting downvoted? I made my account a few minutes ago to ask this question, but I think I have provided enough information, and I can't find this question on Stack Overflow.

Answer

Here's how I'd prepare the output you want:

def print_sudoku(board):
    print("-"*37)
    for i, row in enumerate(board):
        print(("|" + " {}   {}   {} |"*3).format(*[x if x != 0 else " " for x in row]))
        if i == 8:
            print("-"*37)
        elif i % 3 == 2:
            print("|" + "---+"*8 + "---|")
        else:
            print("|" + "   +"*8 + "   |")

You could simplify things a bit if were willing to have plus signs in the border:

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

is produced by:

def print_sudoku2(board):
    print("+" + "---+"*9)
    for i, row in enumerate(board):
        print(("|" + " {}   {}   {} |"*3).format(*[x if x != 0 else " " for x in row]))
        if i % 3 == 2:
            print("+" + "---+"*9)
        else:
            print("+" + "   +"*9)