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

Method insert_north

other/word_search.py:36–66  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

34 self.board: list[list[str | None]] = [[None] * width for _ in range(height)]
35
36 def insert_north(self, word: str, rows: list[int], cols: list[int]) -> None:
37 """
38 >>> ws = WordSearch(WORDS, 3, 3)
39 >>> ws.insert_north("cat", [2], [2])
40 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
41 [[None, None, 't'],
42 [None, None, 'a'],
43 [None, None, 'c']]
44 >>> ws.insert_north("at", [0, 1, 2], [2, 1])
45 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
46 [[None, 't', 't'],
47 [None, 'a', 'a'],
48 [None, None, 'c']]
49 """
50 word_length = len(word)
51 # Attempt to insert the word into each row and when successful, exit
52 for row in rows:
53 # Check if there is space above the row to fit in the word
54 if word_length > row + 1:
55 continue
56
57 # Attempt to insert the word into each column
58 for col in cols:
59 # Only check to be made here is if there are existing letters
60 # above the column that will be overwritten
61 letters_above = [self.board[row - i][col] for i in range(word_length)]
62 if all(letter is None for letter in letters_above):
63 # Successful, insert the word north
64 for i in range(word_length):
65 self.board[row - i][col] = word[i]
66 return
67
68 def insert_northeast(self, word: str, rows: list[int], cols: list[int]) -> None:
69 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected