Skip to content

Create crossword_puzzle_solver.py #11011

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

Merged
merged 27 commits into from
Oct 28, 2023
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6f90a16
Create crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
e2d5a02
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
1feac57
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
4781e0b
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
2a78274
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
d950484
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
3ec196d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
82256da
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
f4cd628
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
3684f44
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 26, 2023
3bdeefe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2023
0571200
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 27, 2023
88ee942
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
9448cc2
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 27, 2023
49f6e4d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
236b159
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 27, 2023
5142ce2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
e37a7c9
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 27, 2023
77863f6
Update backtracking/crossword_puzzle_solver.py
cclauss Oct 28, 2023
ecac1fd
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 28, 2023
76dd3fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
947e09c
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 28, 2023
f211c61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
f0239ae
Update crossword_puzzle_solver.py
Khushi-Shukla Oct 28, 2023
ac0f723
Apply suggestions from code review
cclauss Oct 28, 2023
13794aa
Update crossword_puzzle_solver.py
cclauss Oct 28, 2023
e6bd599
Update crossword_puzzle_solver.py
cclauss Oct 28, 2023
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
132 changes: 132 additions & 0 deletions backtracking/crossword_puzzle_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# https://www.geeksforgeeks.org/solve-crossword-puzzle/


def 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
"""
for i in range(len(word)):
if vertical:
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
return False
else:
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
return False
return True


def 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', '', '', '']]
"""
for i, char in enumerate(word):
if vertical:
puzzle[row + i][col] = char
else:
puzzle[row][col + i] = char


def 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
[['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
"""
for i in range(len(word)):
if vertical:
puzzle[row + i][col] = ""
else:
puzzle[row][col + i] = ""


def 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
"""
for row in range(len(puzzle)):
for col in range(len(puzzle[0])):
if puzzle[row][col] == "":
for word in words:
for vertical in [True, False]:
if is_valid(puzzle, word, row, col, vertical):
place_word(puzzle, word, row, col, vertical)
words.remove(word)
if solve_crossword(puzzle, words):
return True
words.append(word)
remove_word(puzzle, word, row, col, vertical)
return False
return True


if __name__ == "__main__":
PUZZLE = [[""] * 3 for _ in range(3)]
WORDS = ["cat", "dog", "car"]

if solve_crossword(PUZZLE, WORDS):
print("Solution found:")
for row in PUZZLE:
print(" ".join(row))
else:
print("No solution found:")