Skip to content

Update docstrings in the functions definitions. #11796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions data_structures/arrays/sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@


def cross(items_a, items_b):
"Cross product of elements in A and elements in B."
"""
Cross product of elements in A and elements in B.
"""
return [a + b for a in items_a for b in items_b]


Expand All @@ -27,7 +29,7 @@ def cross(items_a, items_b):


def test():
"A set of unit tests."
"""A set of unit tests."""
assert len(squares) == 81
assert len(unitlist) == 27
assert all(len(units[s]) == 3 for s in squares)
Expand All @@ -47,8 +49,10 @@ def test():


def parse_grid(grid):
"""Convert grid to a dict of possible values, {square: digits}, or
return False if a contradiction is detected."""
"""
Convert grid to a dict of possible values, {square: digits}, or
return False if a contradiction is detected.
"""
## To start, every square can be any digit; then assign values from the grid.
values = {s: digits for s in squares}
for s, d in grid_values(grid).items():
Expand All @@ -58,15 +62,19 @@ def parse_grid(grid):


def grid_values(grid):
"Convert grid into a dict of {square: char} with '0' or '.' for empties."
"""
Convert grid into a dict of {square: char} with '0' or '.' for empties.
"""
chars = [c for c in grid if c in digits or c in "0."]
assert len(chars) == 81
return dict(zip(squares, chars))


def assign(values, s, d):
"""Eliminate all the other values (except d) from values[s] and propagate.
Return values, except return False if a contradiction is detected."""
"""
Eliminate all the other values (except d) from values[s] and propagate.
Return values, except return False if a contradiction is detected.
"""
other_values = values[s].replace(d, "")
if all(eliminate(values, s, d2) for d2 in other_values):
return values
Expand All @@ -75,8 +83,10 @@ def assign(values, s, d):


def eliminate(values, s, d):
"""Eliminate d from values[s]; propagate when values or places <= 2.
Return values, except return False if a contradiction is detected."""
"""
Eliminate d from values[s]; propagate when values or places <= 2.
Return values, except return False if a contradiction is detected.
"""
if d not in values[s]:
return values ## Already eliminated
values[s] = values[s].replace(d, "")
Expand All @@ -99,7 +109,9 @@ def eliminate(values, s, d):


def display(values):
"Display these values as a 2-D grid."
"""
Display these values as a 2-D grid.
"""
width = 1 + max(len(values[s]) for s in squares)
line = "+".join(["-" * (width * 3)] * 3)
for r in rows:
Expand All @@ -114,19 +126,24 @@ def display(values):


def solve(grid):
"""
Solve the grid.
"""
return search(parse_grid(grid))


def some(seq):
"Return some element of seq that is true."
"""Return some element of seq that is true."""
for e in seq:
if e:
return e
return False


def search(values):
"Using depth-first search and propagation, try all possible values."
"""
Using depth-first search and propagation, try all possible values.
"""
if values is False:
return False ## Failed earlier
if all(len(values[s]) == 1 for s in squares):
Expand All @@ -137,9 +154,11 @@ def search(values):


def solve_all(grids, name="", showif=0.0):
"""Attempt to solve a sequence of grids. Report results.
"""
Attempt to solve a sequence of grids. Report results.
When showif is a number of seconds, display puzzles that take longer.
When showif is None, don't display any puzzles."""
When showif is None, don't display any puzzles.
"""

def time_solve(grid):
start = time.monotonic()
Expand All @@ -162,7 +181,9 @@ def time_solve(grid):


def solved(values):
"A puzzle is solved if each unit is a permutation of the digits 1 to 9."
"""
A puzzle is solved if each unit is a permutation of the digits 1 to 9.
"""

def unitsolved(unit):
return {values[s] for s in unit} == set(digits)
Expand All @@ -176,9 +197,11 @@ def from_file(filename, sep="\n"):


def random_puzzle(assignments=17):
"""Make a random puzzle with N or more assignments. Restart on contradictions.
"""
Make a random puzzle with N or more assignments. Restart on contradictions.
Note the resulting puzzle is not guaranteed to be solvable, but empirically
about 99.8% of them are solvable. Some have multiple solutions."""
about 99.8% of them are solvable. Some have multiple solutions.
"""
values = {s: digits for s in squares}
for s in shuffled(squares):
if not assign(values, s, random.choice(values[s])):
Expand All @@ -190,7 +213,9 @@ def random_puzzle(assignments=17):


def shuffled(seq):
"Return a randomly shuffled copy of the input sequence."
"""
Return a randomly shuffled copy of the input sequence.
"""
seq = list(seq)
random.shuffle(seq)
return seq
Expand Down