Skip to content

Instantly share code, notes, and snippets.

@mineshpatel1
Last active October 11, 2017 12:06
Show Gist options
  • Save mineshpatel1/2c370ebdb0e68eb61686c01f7122ab11 to your computer and use it in GitHub Desktop.
Save mineshpatel1/2c370ebdb0e68eb61686c01f7122ab11 to your computer and use it in GitHub Desktop.
Sudoku Solver 1 - Appendix 1 #blog #bernard
def display_grid(grid, coords=False):
"""
Displays a 9x9 soduku grid in a nicely formatted way.
Args:
grid (str|dict|list): A string representing a Sudoku grid. Valid characters are digits from 1-9 and empty squares are
specified by 0 or . only. Any other characters are ignored. A `ValueError` will be raised if the input does
not specify exactly 81 valid grid positions.
Can accept a dictionary where each key is the position on the board from A1 to I9.
Can accept a list of strings or integers with empty squares represented by 0.
coords (bool): Optionally prints the coordinate labels.
Returns:
str: Formatted depiction of a 9x9 soduku grid.
"""
if grid is None or grid is False:
return None
all_rows = 'ABCDEFGHI'
all_cols = '123456789'
null_chars = '0.'
if type(grid) == str:
grid = parse_puzzle(grid)
elif type(grid) == list:
grid = parse_puzzle(''.join([str(el) for el in grid]))
width = max([3, max([len(grid[pos]) for pos in grid]) + 1])
display = ''
if coords:
display += ' ' + ''.join([all_cols[i].center(width) for i in range(3)]) + '|'
display += ''.join([all_cols[i].center(width) for i in range(3, 6)]) + '|'
display += ''.join([all_cols[i].center(width) for i in range(6, 9)]) + '\n '
display += '--' + ''.join(['-' for x in range(width * 9)]) + '\n'
row_counter = 0
col_counter = 0
for row in all_rows:
if coords:
display += all_rows[row_counter] + ' |'
row_counter += 1
for col in all_cols:
col_counter += 1
if grid[row + col] in null_chars:
grid[row + col] = '.'
display += ('%s' % grid[row + col]).center(width)
if col_counter % 3 == 0 and col_counter % 9 != 0:
display += '|'
if col_counter % 9 == 0:
display += '\n'
if row_counter % 3 == 0 and row_counter != 9:
if coords:
display += ' |'
display += '+'.join([''.join(['-' for x in range(width * 3)]) for y in range(3)]) + '\n'
print(display)
return display
# Easy Sudoku Puzzle
puzzle1 = """
1 . 5 | . 7 . | 4 . .
. 8 . | 2 . . | . . .
7 2 4 | . . 1 | . . 6
---------+---------+---------
. . . | 3 2 5 | . . .
2 3 7 | . . . | 1 4 5
6 . . | 4 1 7 | . . .
---------+---------+---------
8 . . | 1 . . | 6 2 4
. . . | . . 3 | . 5 .
. . 1 | . 4 . | 3 . 9
"""
# Arto Inkala's 2012 Puzzle
puzzle = """
8 . . | . . . | . . .
. . 3 | 6 . . | . . .
. 7 . | . 9 . | 2 . .
---------+---------+---------
. 5 . | . . 7 | . . .
. . . | . 4 5 | 7 . .
. . . | 1 . . | . 3 .
---------+---------+---------
. . 1 | . . . | . 6 8
. . 8 | 5 . . | . 1 .
. 9 . | . . . | 4 . .
"""
def validate_sudoku(puzzle):
"""Checks if a completed Sudoku puzzle has a valid solution."""
if puzzle is None:
return False
coords, groups, all_units = sudoku_reference()
full = [str(x) for x in range(1, 10)] # Full set, 1-9 as strings
# Checks if all units contain a full set
return all([sorted([puzzle[cell] for cell in unit]) == full for unit in all_units])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment