Skip to content

Commit

Permalink
neue Lösung
Browse files Browse the repository at this point in the history
  • Loading branch information
hakansaglam29 committed Oct 23, 2020
1 parent 73757d6 commit 54f8e07
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "yapf"
}
27 changes: 16 additions & 11 deletions 030_sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@
# the digits from 1 to 9.

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

def valid_solution(board):
dik = [list(map(lambda x:x[a],board)) for a in range(len(board))]
# dik = []
# for a in range(len(board))
# dik.extend(list(map(lambda x:x[a],board)))
print(dik)
k_dokuz=[list(map(lambda x:x[i:i+3],board[k:k+3])) for i in range(0,9,3)\
for k in range(0,9,3)]
# dik.extend(list(map(lambda x:x[a],board)))
k_dokuz=[list(map(lambda x:x[i:i+3],board[k:k+3])) for k in range(0,9,3)\
for i in range(0,9,3)]

# k_dokuz = []
# for k in range(0,9,3):
# for i in range(0,9,3):
# k_dokuz.extend(list(map(lambda x:x[i:i+3],board[k:k+3])))
print(k_dokuz)

k_dokuz= [i[0]+i[1]+i[2] for i in k_dokuz]

board=[list(map(lambda x:x[i:i+3],k_dokuz[k:k+3])) for k in range(0,9,3)\
for i in range(0,9,3)]

board= [i[0]+i[1]+i[2] for i in board]

# y_k_dokuz = []
# for i in k_dokuz:
# y_k_dokuz += [i[0]+i[1]+i[2]]

for i in board,dik,k_dokuz:
for k in i:
if set(k)!={1,2,3,4,5,6,7,8,9}: return False; break
if set(k)!={1,2,3,4,5,6,7,8,9}:
return False
return True
# print(valid_solution(board))

Expand Down
59 changes: 59 additions & 0 deletions 034_sudoku_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Write a function that will solve a 9x9 Sudoku puzzle. The function will take one argument consisting of the 2D puzzle array, with the value 0 representing an unknown square.







def sudoku(puzzle):
def fix(board):
for i in board:
for k in range(9):
if type(i[k]) == list:
for x in i:
if x in tuple(i[k]): i[k].remove(x)
for i in board:
for k in range(9):
if type(i[k]) == list:
if len(i[k]) == 1: i[k] = i[k][0]

for i in puzzle:
for k in range(len(i)):
if i[k] == 0: i[k]=range(1,10)

board_in_one_line = [type(i[k]) for k in range(9) for i in puzzle]

while len(set(board_in_one_line))==2:

fix(puzzle)

puzzle=[list(map(lambda x:x[a],puzzle)) for a in range(9)]

fix(puzzle)

puzzle=[list(map(lambda x:x[a],puzzle)) for a in range(9)]

puzzle=[list(map(lambda x:x[i:i+3],puzzle[k:k+3])) for k in range(0,9,3) for i in range(0,9,3)]
puzzle= [i[0]+i[1]+i[2] for i in puzzle]

fix(puzzle)

puzzle=[list(map(lambda x:x[i:i+3],puzzle[k:k+3])) for k in range(0,9,3) for i in range(0,9,3)]
puzzle= [i[0]+i[1]+i[2] for i in puzzle]

board_in_one_line = [type(i[k]) for k in range(len(i)) for i in puzzle]

return puzzle

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

print(sudoku(puzzle))
1 change: 1 addition & 0 deletions tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print(k_dokuz)

0 comments on commit 54f8e07

Please sign in to comment.