>>> ws = WordSearch(WORDS, 3, 3) >>> ws.insert_northwest("cat", [2], [2]) >>> ws.board # doctest: +NORMALIZE_WHITESPACE [['t', None, None], [None, 'a', None], [None, None, 'c']] >>> ws.insert_northwest("at", [1, 2], [0, 1]) >>> ws.boa
(self, word: str, rows: list[int], cols: list[int])
| 276 | return |
| 277 | |
| 278 | def insert_northwest(self, word: str, rows: list[int], cols: list[int]) -> None: |
| 279 | """ |
| 280 | >>> ws = WordSearch(WORDS, 3, 3) |
| 281 | >>> ws.insert_northwest("cat", [2], [2]) |
| 282 | >>> ws.board # doctest: +NORMALIZE_WHITESPACE |
| 283 | [['t', None, None], |
| 284 | [None, 'a', None], |
| 285 | [None, None, 'c']] |
| 286 | >>> ws.insert_northwest("at", [1, 2], [0, 1]) |
| 287 | >>> ws.board # doctest: +NORMALIZE_WHITESPACE |
| 288 | [['t', None, None], |
| 289 | ['t', 'a', None], |
| 290 | [None, 'a', 'c']] |
| 291 | """ |
| 292 | word_length = len(word) |
| 293 | # Attempt to insert the word into each row and when successful, exit |
| 294 | for row in rows: |
| 295 | # Check if there is space for the word above the row |
| 296 | if word_length > row + 1: |
| 297 | continue |
| 298 | |
| 299 | # Attempt to insert the word into each column |
| 300 | for col in cols: |
| 301 | # Check if there is space to the left of the word as well as above |
| 302 | if word_length > col + 1: |
| 303 | continue |
| 304 | |
| 305 | # Check if there are existing letters |
| 306 | # to the right of the column that will be overwritten |
| 307 | letters_diagonal_left = [ |
| 308 | self.board[row - i][col - i] for i in range(word_length) |
| 309 | ] |
| 310 | if all(letter is None for letter in letters_diagonal_left): |
| 311 | # Successful, insert the word northwest |
| 312 | for i in range(word_length): |
| 313 | self.board[row - i][col - i] = word[i] |
| 314 | return |
| 315 | |
| 316 | def generate_board(self) -> None: |
| 317 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected