Skip to content

Commit 49f6e4d

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 9448cc2 commit 49f6e4d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

backtracking/crossword_puzzle_solver.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# https://www.geeksforgeeks.org/solve-crossword-puzzle/
22

3+
34
def is_valid(puzzle: list[list[str]], word: str, row: int, col: int) -> bool:
45
"""
56
Check if a word can be placed at the given position.
@@ -9,10 +10,11 @@ def is_valid(puzzle: list[list[str]], word: str, row: int, col: int) -> bool:
910
True
1011
"""
1112
for i in range(len(word)):
12-
if row + i >= len(puzzle) or puzzle[row + i][col] != '':
13+
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
1314
return False
1415
return True
1516

17+
1618
def place_word(puzzle: list[list[str]], word: str, row: int, col: int) -> None:
1719
"""
1820
Place a word at the given position.
@@ -25,6 +27,7 @@ def place_word(puzzle: list[list[str]], word: str, row: int, col: int) -> None:
2527
for i in range(len(word)):
2628
puzzle[row + i][col] = word[i]
2729

30+
2831
def remove_word(puzzle: list[list[str]], word: str, row: int, col: int) -> None:
2932
"""
3033
Remove a word from the given position.
@@ -35,7 +38,8 @@ def remove_word(puzzle: list[list[str]], word: str, row: int, col: int) -> None:
3538
[['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']]
3639
"""
3740
for i in range(len(word)):
38-
puzzle[row + i][col] = ''
41+
puzzle[row + i][col] = ""
42+
3943

4044
def solve_crossword(puzzle: list[list[str]], words: list[str]) -> bool:
4145
"""
@@ -48,7 +52,7 @@ def solve_crossword(puzzle: list[list[str]], words: list[str]) -> bool:
4852
"""
4953
for row in range(len(puzzle)):
5054
for col in range(len(puzzle[0])):
51-
if puzzle[row][col] == '':
55+
if puzzle[row][col] == "":
5256
for word in words:
5357
if is_valid(puzzle, word, row, col):
5458
place_word(puzzle, word, row, col)
@@ -60,13 +64,14 @@ def solve_crossword(puzzle: list[list[str]], words: list[str]) -> bool:
6064
return False
6165
return True
6266

67+
6368
# Replace with your actual puzzle and words
64-
PUZZLE = [['' for _ in range(3)] for _ in range(3)]
65-
WORDS = ['cat', 'dog', 'pig']
69+
PUZZLE = [["" for _ in range(3)] for _ in range(3)]
70+
WORDS = ["cat", "dog", "pig"]
6671

6772
if solve_crossword(PUZZLE, WORDS):
68-
print('Solution found:')
73+
print("Solution found:")
6974
else:
70-
print('No solution found:')
75+
print("No solution found:")
7176
for row in PUZZLE:
72-
print(' '.join(row))
77+
print(" ".join(row))

0 commit comments

Comments
 (0)