backtracking.crossword_puzzle_solver

Attributes

PUZZLE

Functions

is_valid(→ bool)

Check if a word can be placed at the given position.

place_word(→ None)

Place a word at the given position.

remove_word(→ None)

Remove a word from the given position.

solve_crossword(→ bool)

Solve the crossword puzzle using backtracking.

Module Contents

backtracking.crossword_puzzle_solver.is_valid(puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool) bool

Check if a word can be placed at the given position.

>>> puzzle = [
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', '']
... ]
>>> is_valid(puzzle, 'word', 0, 0, True)
True
>>> puzzle = [
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', '']
... ]
>>> is_valid(puzzle, 'word', 0, 0, False)
True
backtracking.crossword_puzzle_solver.place_word(puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool) None

Place a word at the given position.

>>> puzzle = [
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', '']
... ]
>>> place_word(puzzle, 'word', 0, 0, True)
>>> puzzle
[['w', '', '', ''], ['o', '', '', ''], ['r', '', '', ''], ['d', '', '', '']]
backtracking.crossword_puzzle_solver.remove_word(puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool) None

Remove a word from the given position.

>>> puzzle = [
...     ['w', '', '', ''],
...     ['o', '', '', ''],
...     ['r', '', '', ''],
...     ['d', '', '', '']
... ]
>>> remove_word(puzzle, 'word', 0, 0, True)
>>> puzzle
[['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
backtracking.crossword_puzzle_solver.solve_crossword(puzzle: list[list[str]], words: list[str]) bool

Solve the crossword puzzle using backtracking.

>>> puzzle = [
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', '']
... ]
>>> words = ['word', 'four', 'more', 'last']
>>> solve_crossword(puzzle, words)
True
>>> puzzle = [
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', ''],
...     ['', '', '', '']
... ]
>>> words = ['word', 'four', 'more', 'paragraphs']
>>> solve_crossword(puzzle, words)
False
backtracking.crossword_puzzle_solver.PUZZLE