In [1]:
KOLOMMEN = 4
TEKENS = ['1','2','3','4']
def print_sudoku(sudoku):
for i, s in enumerate(sudoku):
s = str(s).center(24)
if ( (i+1) % KOLOMMEN) == 0:
print(s)
else:
print(s,end='')
def lees_sudoku(invoer):
toegestane_invoer = TEKENS + ['.']
# filteren
gefilterd = [teken for teken in invoer if teken in toegestane_invoer]
# vervangen . voor alle tekens, en omzetten naar set
sudoku = [set(TEKENS) if teken == '.' else set(teken) for teken in gefilterd]
return(sudoku)
In [2]:
invoer = """
3 . | 4 .
. 1 | . 3
----+----
2 3 | . .
1 . | . 2
"""
sudoku = lees_sudoku(invoer)
print_sudoku(sudoku)
In [3]:
rijen = """
AAAA
BBBB
CCCC
DDDD
"""
rijen = list(rijen.replace("\n", ""))
In [4]:
rij = """
1111
2222
3333
4444
"""
rij = list(rij.replace("\n", ""))
kolom = """
ABCD
ABCD
ABCD
ABCD
"""
kolom = list(kolom.replace("\n", ""))
blok = """
wwxx
wwxx
yyzz
yyzz
"""
blok = list(blok.replace("\n", ""))
for r,c,b,s in zip(rij,kolom,blok,sudoku):
print(r,c,b,s)
In [5]:
print(sudoku[1])
rij[1]
Out[5]:
In [6]:
[s for s, r in zip(sudoku, rij) if r == rij[1]]
Out[6]:
In [7]:
for s, r in zip(sudoku, rij):
if r == rij[1] and len(s) == 1:
sudoku[1] -= s
sudoku[1]
Out[7]:
In [8]:
for s1, r1, c1 ,b1 in zip(sudoku,rij,kolom,blok):
if len(s1) == 1:
continue
for s2, r2, c2 ,b2 in zip(sudoku,rij,kolom,blok):
if s1 is s2:
continue
if len(s2) == 1:
if r1 == r2 or c1 == c2 or b1 == b2:
s1 -= s2
print_sudoku(sudoku)
In [ ]: