MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / word_exists

Function word_exists

backtracking/word_search.py:91–156  ·  view source on GitHub ↗

>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED") True >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE") True >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB") False >>> word_ex

(board: list[list[str]], word: str)

Source from the content-addressed store, hash-verified

89
90
91def word_exists(board: list[list[str]], word: str) -> bool:
92 """
93 >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
94 True
95 >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")
96 True
97 >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")
98 False
99 >>> word_exists([["A"]], "A")
100 True
101 >>> word_exists([["B", "A", "A"], ["A", "A", "A"], ["A", "B", "A"]], "ABB")
102 False
103 >>> word_exists([["A"]], 123)
104 Traceback (most recent call last):
105 ...
106 ValueError: The word parameter should be a string of length greater than 0.
107 >>> word_exists([["A"]], "")
108 Traceback (most recent call last):
109 ...
110 ValueError: The word parameter should be a string of length greater than 0.
111 >>> word_exists([[]], "AB")
112 Traceback (most recent call last):
113 ...
114 ValueError: The board should be a non empty matrix of single chars strings.
115 >>> word_exists([], "AB")
116 Traceback (most recent call last):
117 ...
118 ValueError: The board should be a non empty matrix of single chars strings.
119 >>> word_exists([["A"], [21]], "AB")
120 Traceback (most recent call last):
121 ...
122 ValueError: The board should be a non empty matrix of single chars strings.
123 """
124
125 # Validate board
126 board_error_message = (
127 "The board should be a non empty matrix of single chars strings."
128 )
129
130 len_board = len(board)
131 if not isinstance(board, list) or len(board) == 0:
132 raise ValueError(board_error_message)
133
134 for row in board:
135 if not isinstance(row, list) or len(row) == 0:
136 raise ValueError(board_error_message)
137
138 for item in row:
139 if not isinstance(item, str) or len(item) != 1:
140 raise ValueError(board_error_message)
141
142 # Validate word
143 if not isinstance(word, str) or len(word) == 0:
144 raise ValueError(
145 "The word parameter should be a string of length greater than 0."
146 )
147
148 len_board_column = len(board[0])

Callers

nothing calls this directly

Calls 2

exits_wordFunction · 0.85
get_point_keyFunction · 0.85

Tested by

no test coverage detected