Skip to content

Commit d80ee90

Browse files
Khushi-Shuklapre-commit-ci[bot]cclauss
authored
Create crossword_puzzle_solver.py (#11011)
* Create crossword_puzzle_solver.py * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * Update backtracking/crossword_puzzle_solver.py * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update crossword_puzzle_solver.py * Apply suggestions from code review * Update crossword_puzzle_solver.py * Update crossword_puzzle_solver.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent b51b833 commit d80ee90

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# https://www.geeksforgeeks.org/solve-crossword-puzzle/
2+
3+
4+
def is_valid(
5+
puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool
6+
) -> bool:
7+
"""
8+
Check if a word can be placed at the given position.
9+
10+
>>> puzzle = [
11+
... ['', '', '', ''],
12+
... ['', '', '', ''],
13+
... ['', '', '', ''],
14+
... ['', '', '', '']
15+
... ]
16+
>>> is_valid(puzzle, 'word', 0, 0, True)
17+
True
18+
>>> puzzle = [
19+
... ['', '', '', ''],
20+
... ['', '', '', ''],
21+
... ['', '', '', ''],
22+
... ['', '', '', '']
23+
... ]
24+
>>> is_valid(puzzle, 'word', 0, 0, False)
25+
True
26+
"""
27+
for i in range(len(word)):
28+
if vertical:
29+
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
30+
return False
31+
else:
32+
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
33+
return False
34+
return True
35+
36+
37+
def place_word(
38+
puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool
39+
) -> None:
40+
"""
41+
Place a word at the given position.
42+
43+
>>> puzzle = [
44+
... ['', '', '', ''],
45+
... ['', '', '', ''],
46+
... ['', '', '', ''],
47+
... ['', '', '', '']
48+
... ]
49+
>>> place_word(puzzle, 'word', 0, 0, True)
50+
>>> puzzle
51+
[['w', '', '', ''], ['o', '', '', ''], ['r', '', '', ''], ['d', '', '', '']]
52+
"""
53+
for i, char in enumerate(word):
54+
if vertical:
55+
puzzle[row + i][col] = char
56+
else:
57+
puzzle[row][col + i] = char
58+
59+
60+
def remove_word(
61+
puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool
62+
) -> None:
63+
"""
64+
Remove a word from the given position.
65+
66+
>>> puzzle = [
67+
... ['w', '', '', ''],
68+
... ['o', '', '', ''],
69+
... ['r', '', '', ''],
70+
... ['d', '', '', '']
71+
... ]
72+
>>> remove_word(puzzle, 'word', 0, 0, True)
73+
>>> puzzle
74+
[['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
75+
"""
76+
for i in range(len(word)):
77+
if vertical:
78+
puzzle[row + i][col] = ""
79+
else:
80+
puzzle[row][col + i] = ""
81+
82+
83+
def solve_crossword(puzzle: list[list[str]], words: list[str]) -> bool:
84+
"""
85+
Solve the crossword puzzle using backtracking.
86+
87+
>>> puzzle = [
88+
... ['', '', '', ''],
89+
... ['', '', '', ''],
90+
... ['', '', '', ''],
91+
... ['', '', '', '']
92+
... ]
93+
94+
>>> words = ['word', 'four', 'more', 'last']
95+
>>> solve_crossword(puzzle, words)
96+
True
97+
>>> puzzle = [
98+
... ['', '', '', ''],
99+
... ['', '', '', ''],
100+
... ['', '', '', ''],
101+
... ['', '', '', '']
102+
... ]
103+
>>> words = ['word', 'four', 'more', 'paragraphs']
104+
>>> solve_crossword(puzzle, words)
105+
False
106+
"""
107+
for row in range(len(puzzle)):
108+
for col in range(len(puzzle[0])):
109+
if puzzle[row][col] == "":
110+
for word in words:
111+
for vertical in [True, False]:
112+
if is_valid(puzzle, word, row, col, vertical):
113+
place_word(puzzle, word, row, col, vertical)
114+
words.remove(word)
115+
if solve_crossword(puzzle, words):
116+
return True
117+
words.append(word)
118+
remove_word(puzzle, word, row, col, vertical)
119+
return False
120+
return True
121+
122+
123+
if __name__ == "__main__":
124+
PUZZLE = [[""] * 3 for _ in range(3)]
125+
WORDS = ["cat", "dog", "car"]
126+
127+
if solve_crossword(PUZZLE, WORDS):
128+
print("Solution found:")
129+
for row in PUZZLE:
130+
print(" ".join(row))
131+
else:
132+
print("No solution found:")

0 commit comments

Comments
 (0)