Skip to content

Commit

Permalink
fix: Add typing and format script for no reason at all.
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanelli committed Sep 25, 2023
1 parent 63eadaf commit cc97f60
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
46 changes: 0 additions & 46 deletions sudoku-solver/soduku_solver.py

This file was deleted.

49 changes: 49 additions & 0 deletions sudoku-solver/sudoku_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np
from typing import List


def is_valid(sudoku: np.array, x: int, y: int, value: int) -> bool:
return value not in sudoku[x, :] and value not in sudoku[:, y] and value not in quadrant(sudoku, x, y)


def quadrant(sudoku: np.array, x: int, y: int) -> np.array:
xx = x // 3
yy = y // 3
return sudoku[xx * 3 : (xx + 1) * 3, yy * 3 : (yy + 1) * 3]


def possibilities(sudoku: np.array, x: int, y: int) -> List[int]:
_possibilities = []
for value in range(1, 10):
if is_valid(sudoku, x, y, value):
_possibilities.append(value)
return _possibilities


def solver(sudoku: np.array) -> List[np.array]:
solutions = []
for (x, y), value in np.ndenumerate(sudoku):
if value == 0:
for possibility in possibilities(sudoku, x, y):
sudoku[x, y] = possibility
extra_solutions = solver(sudoku)
solutions.extend(extra_solutions)
sudoku[x, y] = 0
return solutions
solutions.append(sudoku.copy())
return solutions


if __name__ == '__main__':
my_sudoku = np.array([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, 0, 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]).reshape([9, 9])
my_solutions = solver(my_sudoku)
for solution in my_solutions:
print(solution)

0 comments on commit cc97f60

Please sign in to comment.