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

Method insert_southwest

other/word_search.py:208–244  ·  view source on GitHub ↗

>>> ws = WordSearch(WORDS, 3, 3) >>> ws.insert_southwest("cat", [0], [2]) >>> ws.board # doctest: +NORMALIZE_WHITESPACE [[None, None, 'c'], [None, 'a', None], ['t', None, None]] >>> ws.insert_southwest("at", [1, 2], [2, 1, 0]) >>> ws.

(self, word: str, rows: list[int], cols: list[int])

Source from the content-addressed store, hash-verified

206 return
207
208 def insert_southwest(self, word: str, rows: list[int], cols: list[int]) -> None:
209 """
210 >>> ws = WordSearch(WORDS, 3, 3)
211 >>> ws.insert_southwest("cat", [0], [2])
212 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
213 [[None, None, 'c'],
214 [None, 'a', None],
215 ['t', None, None]]
216 >>> ws.insert_southwest("at", [1, 2], [2, 1, 0])
217 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
218 [[None, None, 'c'],
219 [None, 'a', 'a'],
220 ['t', 't', None]]
221 """
222 word_length = len(word)
223 # Attempt to insert the word into each row and when successful, exit
224 for row in rows:
225 # Check if there is space for the word below the row
226 if word_length + row > self.height:
227 continue
228
229 # Attempt to insert the word into each column
230 for col in cols:
231 # Check if there is space to the left of the word as well as below
232 if word_length > col + 1:
233 continue
234
235 # Check if there are existing letters
236 # to the right of the column that will be overwritten
237 letters_diagonal_left = [
238 self.board[row + i][col - i] for i in range(word_length)
239 ]
240 if all(letter is None for letter in letters_diagonal_left):
241 # Successful, insert the word southwest
242 for i in range(word_length):
243 self.board[row + i][col - i] = word[i]
244 return
245
246 def insert_west(self, word: str, rows: list[int], cols: list[int]) -> None:
247 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected