2

i am trying to display a 2D sudoku board in python like this:

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

I managed to display the board without the seperation lines using this code:

rows = 'ABCDEFGHI'
cols = '123456789'

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

values is a dictionary {'A1':'0', 'A2':'0', 'A3':'3', 'A4':'0', 'A5':'2'...etc} I get this output:

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

Any help?

Gpuzz
  • 47
  • 1
  • 4

5 Answers5

2

The following may work. But I think that a function that render a string as a result may be more useful (for writing the result to a text file for example, without too much monkey-patching).

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

Result:

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
Nagasaki45
  • 2,634
  • 1
  • 22
  • 27
1

Here's an approach that's a bit messy. If you add some other identifier to your row and column strings, you actually get the in between columns in a recognizable form:

# 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)

Another possibility is to be more clever and insert the separator modified cells into the dictionary (ie "xx": "+", "Ax": "|"), but that's more work. You can use the get() method of the dictionary to automatically fill in one set of those however (ie default to returning a pipe or hyphen).

thegrinner
  • 11,546
  • 5
  • 41
  • 64
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"

wherever spaces are present in the string , you can replace it with a '-'

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

Not what u are looking for? Let me know

m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • This looks much better than the original approach, but the asker is using a dictionary as the source, and wants to produce the output programmatically. – Wolf Mar 09 '15 at 14:07
0

This should do the trick:

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]
Kyle Coventry
  • 555
  • 3
  • 9
0

Solution for 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()
Skvara
  • 16
  • 4
  • Looks good so far. You should consider to take the values from an dict with 9*9 elements as shown in the question. – Wolf Oct 29 '19 at 12:36
  • You're right, I didn't really answer the question here, apologies. I'll try to edit the answer when I have time. – Skvara Oct 29 '19 at 23:24
  • After changing from `grid` to `values`, you could do something like `values.get(chr(ord('A')+i)+str(j+1), '0')` – Wolf Oct 30 '19 at 08:20