-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6f90a16
Create crossword_puzzle_solver.py
Khushi-Shukla e2d5a02
Update crossword_puzzle_solver.py
Khushi-Shukla 1feac57
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4781e0b
Update crossword_puzzle_solver.py
Khushi-Shukla 2a78274
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d950484
Update crossword_puzzle_solver.py
Khushi-Shukla 3ec196d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82256da
Update crossword_puzzle_solver.py
Khushi-Shukla f4cd628
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3684f44
Update crossword_puzzle_solver.py
Khushi-Shukla 3bdeefe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0571200
Update crossword_puzzle_solver.py
Khushi-Shukla 88ee942
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9448cc2
Update crossword_puzzle_solver.py
Khushi-Shukla 49f6e4d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 236b159
Update crossword_puzzle_solver.py
Khushi-Shukla 5142ce2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e37a7c9
Update crossword_puzzle_solver.py
Khushi-Shukla 77863f6
Update backtracking/crossword_puzzle_solver.py
cclauss ecac1fd
Update crossword_puzzle_solver.py
Khushi-Shukla 76dd3fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 947e09c
Update crossword_puzzle_solver.py
Khushi-Shukla f211c61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f0239ae
Update crossword_puzzle_solver.py
Khushi-Shukla ac0f723
Apply suggestions from code review
cclauss 13794aa
Update crossword_puzzle_solver.py
cclauss e6bd599
Update crossword_puzzle_solver.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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:") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.