Skip to content

Commit

Permalink
add command line functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mushipeas committed Dec 26, 2019
1 parent 41faadc commit ff27983
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,48 @@ By using iteration, we can avoid any need for tail-call optimisation in a recurs

Written for and tested on Python 3.7.5

## Useage
## Running from cmd-line:

### Usage
usage: py_sudoku_solver.py [-h] GRID

Backtracking solver for a sudoku grid, passed in as any 81 length iterable of
strings or numbers Empty items must be none or empty strings / lists Any
element outside of the 1-9 range will be replaced with None Prints the solved
grid, if successful. Otherwise states not solvable.

positional arguments:
GRID String representing unsolved grid of len(81). Empties must be
given as 0's. Spaces and brackets will be ignored.
ie. '[1,2,0,5...3,5]' or '1205...35'


### Example
>python py_sudoku_solver.py "[2, 0, 9, 0, 8, 0, 5, 0, 0, 0, 0, 4, 7, 6, 9, 0, 0, 0, 3, 0, 0, 0, 1, 2, 0, 4, 0, 0, 0, 3, 6, 0, 0, 0, 5,
4, 0, 4, 0, 0, 0, 0, 0, 8, 0, 8, 5, 0, 0, 0, 7, 6, 0, 0, 0, 2, 0, 8, 7, 0, 0, 0, 9, 0, 0, 0, 1, 9, 6, 2, 0, 0, 0, 0, 5, 0, 4, 0, 1, 0, 8]"
Input read as:
[2, 0, 9, 0, 8, 0, 5, 0, 0]
[0, 0, 4, 7, 6, 9, 0, 0, 0]
[3, 0, 0, 0, 1, 2, 0, 4, 0]
[0, 0, 3, 6, 0, 0, 0, 5, 4]
[0, 4, 0, 0, 0, 0, 0, 8, 0]
[8, 5, 0, 0, 0, 7, 6, 0, 0]
[0, 2, 0, 8, 7, 0, 0, 0, 9]
[0, 0, 0, 1, 9, 6, 2, 0, 0]
[0, 0, 5, 0, 4, 0, 1, 0, 8]
Solution required 2479 iterations
Solved!
[2, 6, 9, 3, 8, 4, 5, 1, 7]
[5, 1, 4, 7, 6, 9, 8, 2, 3]
[3, 8, 7, 5, 1, 2, 9, 4, 6]
[1, 9, 3, 6, 2, 8, 7, 5, 4]
[7, 4, 6, 9, 5, 1, 3, 8, 2]
[8, 5, 2, 4, 3, 7, 6, 9, 1]
[6, 2, 1, 8, 7, 5, 4, 3, 9]
[4, 3, 8, 1, 9, 6, 2, 7, 5]
[9, 7, 5, 2, 4, 3, 1, 6, 8]

## Useage as part of a module:

### solve()
The main function in the script is `solve()` which takes an iterable of the sudoku elements as an input, and returns a completed list.
Expand All @@ -19,9 +60,13 @@ Empty items in the input must be `None` or empty strings / lists.
Any element outside of the 1-9 range will also be replaced with `None` by the function.

### print_grid()
The `print_grid()` function can display the list in a more viewable form:
The `printable_grid()` function can generate the list in a more viewable form:

py_soduku_solver.printable_grid(grid: iter) -> list[lists]

Useable as:

py_soduku_solver.print_grid(grid: iter) -> None
print(*printable_grid(grid), sep='\n')

Which outputs:

Expand Down
31 changes: 30 additions & 1 deletion py_sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,33 @@ def printable_grid(grid):
return square_grid

if __name__ == "__main__":
pass
import argparse

parser = argparse.ArgumentParser(description='''
Backtracking solver for a sudoku grid, passed in as any 81 length iterable of strings or numbers
Empty items must be none or empty strings / lists
Any element outside of the 1-9 range will be replaced with None
Prints the solved grid, if successful. Otherwise states not solvable.
''')

parser.add_argument("GRID", help="""
String representing unsolved grid of len(81). Empties must be given as 0's. Spaces and brackets will be ignored.
ie. '[1,2,0,5...3,5]' or '1205...35'
""", type=list)

args = parser.parse_args()

try:
grid = [int(x) for x in args.GRID if x not in '[ ,]']
except ValueError:
print('Please review input argument. See help for syntax.')
else:
print('Input read as:')
print(*printable_grid(grid), sep='\n')
try:
solved_grid = solve(grid)
except:
print('Grid is unsolvable. Please check the input again.')
else:
print('Solved!')
print(*printable_grid(solved_grid), sep='\n')

0 comments on commit ff27983

Please sign in to comment.