Skip to content

Commit c4ec354

Browse files
authored
Implement board and word validation function
Added a new validation function to check the board and word parameters, ensuring proper input types and values. Updated existing function docstrings for clarity and consistency.
1 parent 4ce2ebe commit c4ec354

1 file changed

Lines changed: 76 additions & 32 deletions

File tree

backtracking/word_search.py

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Result:
2626
True
2727
28-
Implementation notes: Use backtracking approach.
28+
Implementation notes: Use a backtracking approach.
2929
At each point, check all neighbors to try to find the next letter of the word.
3030
3131
leetcode: https://leetcode.com/problems/word-search/
@@ -53,7 +53,7 @@ def exits_word(
5353
visited_points_set: set[int],
5454
) -> bool:
5555
"""
56-
Return True if it's possible to search the word suffix
56+
Return True if it's possible to search for the word suffix
5757
starting from the word_index.
5858
5959
>>> exits_word([["A"]], "B", 0, 0, 0, set())
@@ -88,10 +88,75 @@ def exits_word(
8888
return False
8989

9090

91+
def validate_board_and_word(board: list[list[str]], word: str) -> None:
92+
"""
93+
>>> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
94+
>>> validate_board_and_word(board, "ABCCED")
95+
>>> validate_board_and_word(board, "SEE")
96+
>>> validate_board_and_word(board, "ABCB")
97+
>>> validate_board_and_word([["A"]], "A")
98+
>>> validate_board_and_word([["B", "A", "A"], ["A", "A", "A"], ["A", "B", "A"]], "ABB")
99+
>>> validate_board_and_word([["A"]], 123)
100+
Traceback (most recent call last):
101+
...
102+
ValueError: The word parameter should be a string of length greater than 0.
103+
>>> validate_board_and_word([["A"]], "")
104+
Traceback (most recent call last):
105+
...
106+
ValueError: The word parameter should be a string of length greater than 0.
107+
>>> validate_board_and_word([[]], "AB")
108+
Traceback (most recent call last):
109+
...
110+
ValueError: The board should be a non-empty matrix of single-character strings.
111+
>>> validate_board_and_word([], "AB")
112+
Traceback (most recent call last):
113+
...
114+
ValueError: The board should be a non-empty matrix of single-character strings.
115+
>>> validate_board_and_word([["A"], [21]], "AB")
116+
Traceback (most recent call last):
117+
...
118+
ValueError: The board should be a non-empty matrix of single-character strings.
119+
"""
120+
121+
# Validate board
122+
msg = "The board should be a non-empty matrix of single-character strings."
123+
if not board or not isinstance(board, list):
124+
raise ValueError(msg)
125+
126+
for row in board:
127+
if not row or not isinstance(row, list):
128+
raise ValueError(msg)
129+
130+
for item in row:
131+
if not item or not isinstance(item, str):
132+
raise ValueError(msg)
133+
134+
# Validate word
135+
if not isinstance(word, str) or len(word) == 0:
136+
msg = "The word parameter should be a string of length greater than 0."
137+
raise ValueError(msg)
138+
139+
91140
def get_word_path(board: list[list[str]], word: str) -> list[tuple[int, int]] | None:
141+
"""
142+
Return the path of the word in the board if it exists; otherwise, return None.
143+
144+
>>> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
145+
>>> get_word_path(board, "ABCCED")
146+
[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1)]
147+
>>> get_word_path(board, "SEE")
148+
[(1, 3), (2, 3), (2, 2)]
149+
>>> get_word_path(board, "ABCB") is None
150+
True
151+
>>> get_word_path([["A"]], 123)
152+
Traceback (most recent call last):
153+
...
154+
ValueError: The word parameter should be a string of length greater than 0.
155+
"""
156+
validate_board_and_word(board, word)
92157
rows, cols = len(board), len(board[0])
93158

94-
def backtrack(r, c, index, path, visited):
159+
def backtrack(r: int, c: int, index: int, path: list[tuple[int, int]], visited: set[tuple[int, int]]) -> list[tuple[int, int]] | None:
95160
if board[r][c] != word[index]:
96161
return None
97162

@@ -125,11 +190,12 @@ def backtrack(r, c, index, path, visited):
125190

126191
def word_exists(board: list[list[str]], word: str) -> bool:
127192
"""
128-
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
193+
>>> board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]
194+
>>> word_exists(board, "ABCCED")
129195
True
130-
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")
196+
>>> word_exists(board, "SEE")
131197
True
132-
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")
198+
>>> word_exists(board, "ABCB")
133199
False
134200
>>> word_exists([["A"]], "A")
135201
True
@@ -146,40 +212,18 @@ def word_exists(board: list[list[str]], word: str) -> bool:
146212
>>> word_exists([[]], "AB")
147213
Traceback (most recent call last):
148214
...
149-
ValueError: The board should be a non empty matrix of single chars strings.
215+
ValueError: The board should be a non-empty matrix of single-character strings.
150216
>>> word_exists([], "AB")
151217
Traceback (most recent call last):
152218
...
153-
ValueError: The board should be a non empty matrix of single chars strings.
219+
ValueError: The board should be a non-empty matrix of single-character strings.
154220
>>> word_exists([["A"], [21]], "AB")
155221
Traceback (most recent call last):
156222
...
157-
ValueError: The board should be a non empty matrix of single chars strings.
223+
ValueError: The board should be a non-empty matrix of single-character strings.
158224
"""
159-
160-
# Validate board
161-
board_error_message = (
162-
"The board should be a non empty matrix of single chars strings."
163-
)
164-
225+
validate_board_and_word(board, word)
165226
len_board = len(board)
166-
if not isinstance(board, list) or len(board) == 0:
167-
raise ValueError(board_error_message)
168-
169-
for row in board:
170-
if not isinstance(row, list) or len(row) == 0:
171-
raise ValueError(board_error_message)
172-
173-
for item in row:
174-
if not isinstance(item, str) or len(item) != 1:
175-
raise ValueError(board_error_message)
176-
177-
# Validate word
178-
if not isinstance(word, str) or len(word) == 0:
179-
raise ValueError(
180-
"The word parameter should be a string of length greater than 0."
181-
)
182-
183227
len_board_column = len(board[0])
184228
for i in range(len_board):
185229
for j in range(len_board_column):

0 commit comments

Comments
 (0)