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

Method insert_west

other/word_search.py:246–276  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

244 return
245
246 def insert_west(self, word: str, rows: list[int], cols: list[int]) -> None:
247 """
248 >>> ws = WordSearch(WORDS, 3, 3)
249 >>> ws.insert_west("cat", [1], [2])
250 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
251 [[None, None, None],
252 ['t', 'a', 'c'],
253 [None, None, None]]
254 >>> ws.insert_west("at", [1, 0], [1, 2, 0])
255 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
256 [['t', 'a', None],
257 ['t', 'a', 'c'],
258 [None, None, None]]
259 """
260 word_length = len(word)
261 # Attempt to insert the word into each row and when successful, exit
262 for row in rows:
263 # Attempt to insert the word into each column
264 for col in cols:
265 # Check if there is space to the left of the word
266 if word_length > col + 1:
267 continue
268
269 # Check if there are existing letters
270 # to the left of the column that will be overwritten
271 letters_left = [self.board[row][col - i] for i in range(word_length)]
272 if all(letter is None for letter in letters_left):
273 # Successful, insert the word west
274 for i in range(word_length):
275 self.board[row][col - i] = word[i]
276 return
277
278 def insert_northwest(self, word: str, rows: list[int], cols: list[int]) -> None:
279 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected