From 54f8e078231835379063dd2cd7999c81dd279b70 Mon Sep 17 00:00:00 2001 From: Hakan <67585490+hakansaglam29@users.noreply.github.com> Date: Fri, 23 Oct 2020 13:40:32 +0200 Subject: [PATCH] =?UTF-8?q?neue=20L=C3=B6sung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +++ 030_sudoku.py | 27 ++++++++++++-------- 034_sudoku_solver.py | 59 +++++++++++++++++++++++++++++++++++++++++++ tempCodeRunnerFile.py | 1 + 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 034_sudoku_solver.py create mode 100644 tempCodeRunnerFile.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2b7e46d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "yapf" +} \ No newline at end of file diff --git a/030_sudoku.py b/030_sudoku.py index f4eafd4..4d8a8fd 100644 --- a/030_sudoku.py +++ b/030_sudoku.py @@ -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)) diff --git a/034_sudoku_solver.py b/034_sudoku_solver.py new file mode 100644 index 0000000..e2e419a --- /dev/null +++ b/034_sudoku_solver.py @@ -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)) \ No newline at end of file diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 0000000..5204aea --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1 @@ +print(k_dokuz) \ No newline at end of file