Skip to content

Commit f9cc252

Browse files
Reduce the complexity of backtracking/word_search.py (TheAlgorithms#8166)
* Lower the --max-complexity threshold in the file .flake8
1 parent 9e28ecc commit f9cc252

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

backtracking/word_search.py

+60-52
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,61 @@
3333
"""
3434

3535

36+
def get_point_key(len_board: int, len_board_column: int, row: int, column: int) -> int:
37+
"""
38+
Returns the hash key of matrix indexes.
39+
40+
>>> get_point_key(10, 20, 1, 0)
41+
200
42+
"""
43+
44+
return len_board * len_board_column * row + column
45+
46+
47+
def exits_word(
48+
board: list[list[str]],
49+
word: str,
50+
row: int,
51+
column: int,
52+
word_index: int,
53+
visited_points_set: set[int],
54+
) -> bool:
55+
"""
56+
Return True if it's possible to search the word suffix
57+
starting from the word_index.
58+
59+
>>> exits_word([["A"]], "B", 0, 0, 0, set())
60+
False
61+
"""
62+
63+
if board[row][column] != word[word_index]:
64+
return False
65+
66+
if word_index == len(word) - 1:
67+
return True
68+
69+
traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
70+
len_board = len(board)
71+
len_board_column = len(board[0])
72+
for direction in traverts_directions:
73+
next_i = row + direction[0]
74+
next_j = column + direction[1]
75+
if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
76+
continue
77+
78+
key = get_point_key(len_board, len_board_column, next_i, next_j)
79+
if key in visited_points_set:
80+
continue
81+
82+
visited_points_set.add(key)
83+
if exits_word(board, word, next_i, next_j, word_index + 1, visited_points_set):
84+
return True
85+
86+
visited_points_set.remove(key)
87+
88+
return False
89+
90+
3691
def word_exists(board: list[list[str]], word: str) -> bool:
3792
"""
3893
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
@@ -77,6 +132,8 @@ def word_exists(board: list[list[str]], word: str) -> bool:
77132
board_error_message = (
78133
"The board should be a non empty matrix of single chars strings."
79134
)
135+
136+
len_board = len(board)
80137
if not isinstance(board, list) or len(board) == 0:
81138
raise ValueError(board_error_message)
82139

@@ -94,61 +151,12 @@ def word_exists(board: list[list[str]], word: str) -> bool:
94151
"The word parameter should be a string of length greater than 0."
95152
)
96153

97-
traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
98-
len_word = len(word)
99-
len_board = len(board)
100154
len_board_column = len(board[0])
101-
102-
# Returns the hash key of matrix indexes.
103-
def get_point_key(row: int, column: int) -> int:
104-
"""
105-
>>> len_board=10
106-
>>> len_board_column=20
107-
>>> get_point_key(0, 0)
108-
200
109-
"""
110-
111-
return len_board * len_board_column * row + column
112-
113-
# Return True if it's possible to search the word suffix
114-
# starting from the word_index.
115-
def exits_word(
116-
row: int, column: int, word_index: int, visited_points_set: set[int]
117-
) -> bool:
118-
"""
119-
>>> board=[["A"]]
120-
>>> word="B"
121-
>>> exits_word(0, 0, 0, set())
122-
False
123-
"""
124-
125-
if board[row][column] != word[word_index]:
126-
return False
127-
128-
if word_index == len_word - 1:
129-
return True
130-
131-
for direction in traverts_directions:
132-
next_i = row + direction[0]
133-
next_j = column + direction[1]
134-
if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
135-
continue
136-
137-
key = get_point_key(next_i, next_j)
138-
if key in visited_points_set:
139-
continue
140-
141-
visited_points_set.add(key)
142-
if exits_word(next_i, next_j, word_index + 1, visited_points_set):
143-
return True
144-
145-
visited_points_set.remove(key)
146-
147-
return False
148-
149155
for i in range(len_board):
150156
for j in range(len_board_column):
151-
if exits_word(i, j, 0, {get_point_key(i, j)}):
157+
if exits_word(
158+
board, word, i, j, 0, {get_point_key(len_board, len_board_column, i, j)}
159+
):
152160
return True
153161

154162
return False

0 commit comments

Comments
 (0)