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

Method insert_south

other/word_search.py:176–206  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

174 return
175
176 def insert_south(self, word: str, rows: list[int], cols: list[int]) -> None:
177 """
178 >>> ws = WordSearch(WORDS, 3, 3)
179 >>> ws.insert_south("cat", [0], [0])
180 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
181 [['c', None, None],
182 ['a', None, None],
183 ['t', None, None]]
184 >>> ws.insert_south("at", [2, 1, 0], [0, 1, 2])
185 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
186 [['c', None, None],
187 ['a', 'a', None],
188 ['t', 't', None]]
189 """
190 word_length = len(word)
191 # Attempt to insert the word into each row and when successful, exit
192 for row in rows:
193 # Check if there is space below the row to fit in the word
194 if word_length + row > self.height:
195 continue
196
197 # Attempt to insert the word into each column
198 for col in cols:
199 # Only check to be made here is if there are existing letters
200 # below the column that will be overwritten
201 letters_below = [self.board[row + i][col] for i in range(word_length)]
202 if all(letter is None for letter in letters_below):
203 # Successful, insert the word south
204 for i in range(word_length):
205 self.board[row + i][col] = word[i]
206 return
207
208 def insert_southwest(self, word: str, rows: list[int], cols: list[int]) -> None:
209 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected