Skip to content

Commit 1cc386b

Browse files
committed
Version 0.10.0
Critical fix for possible memory hog bug in the connection to the Dancing Links solver.
1 parent e9b6407 commit 1cc386b

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ Sudoku Solver written in pure Python with no dependencies.
1111
It solves Sudokus of sizes `N x N` by pure induction as
1212
far as is possible, and then uses an optional
1313
[Dancing Links](https://en.wikipedia.org/wiki/Dancing_Links)
14-
solver, which is a brute force methodology, when the basic
15-
induction is not enough.
14+
brute force solver, when the basic induction is not enough.
1615

1716
## Installation
1817

19-
Install by calling:
18+
Install with pip:
2019

2120
pip install dlxsudoku
2221

@@ -38,7 +37,6 @@ from dlxsudoku import Sudoku
3837

3938
s = Sudoku.load_file('path/to/sudoku.sud')
4039
s.solve(verbose=True, allow_brute_force=True)
41-
4240
```
4341

4442
Alternatively, if your Sudoku is stored in string variable
@@ -68,6 +66,24 @@ print(s2)
6866

6967
```
7068

69+
**DLXSudoko treats a Sudoku with multiple solutions as a faulty one
70+
and raises a** ``dlxsudoku.exceptions.SudokuHasMultipleSolutionsError``
71+
**exception in such a situation.**
72+
73+
### Use from terminal
74+
75+
DLXSudoku also installs a console entry point. Can solve Sudokus from string or from path:
76+
77+
```shell
78+
solve-sudoku --sudoku 030467050920010006067300148301006027400850600090200400005624001203000504040030702
79+
```
80+
81+
or
82+
83+
```shell
84+
solve-sudoku --path "path/to/sudoku.sud"
85+
```
86+
7187
### Sudoku formatting
7288

7389
A Sudoku file or string should be structured in the following manner:

dlxsudoku/sudoku.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,22 @@ def solve(self, verbose=False, allow_brute_force=True):
226226
# Else, if singles_found is True, run another iteration to see if new singles have shown up.
227227
if not singles_found:
228228
if allow_brute_force:
229+
solution = None
229230
try:
230231
dlxs = DancingLinksSolver(copy.deepcopy(self._matrix))
231-
solutions = list(dlxs.solve())
232-
except:
232+
solutions = dlxs.solve()
233+
solution = next(solutions)
234+
more_solutions = next(solutions)
235+
except StopIteration as e:
236+
if solution is not None:
237+
self._matrix = solution
238+
else:
239+
raise SudokuHasNoSolutionError("Dancing Links solver could not find any solution.")
240+
except Exception as e:
233241
raise SudokuHasNoSolutionError("Brute Force method failed.")
234-
if len(solutions) == 1:
235-
self._matrix = solutions[0]
236-
elif len(solutions) > 1:
242+
else:
243+
# We end up here if the second `next(solutions)` works,
244+
# i.e. if multiple solutions exist.
237245
raise SudokuHasMultipleSolutionsError("This Sudoku has multiple solutions!")
238246
self.solution_steps.append("BRUTE FORCE - Dancing Links")
239247
break

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
setup(
3232
name='dlxsudoku',
33-
version='0.9.3',
33+
version='0.10.0',
3434
author='Henrik Blidh',
3535
author_email='[email protected]',
3636
description='Sudoku Solver in pure Python with no dependencies',

0 commit comments

Comments
 (0)