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

Method insert_southeast

other/word_search.py:138–174  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

136 return
137
138 def insert_southeast(self, word: str, rows: list[int], cols: list[int]) -> None:
139 """
140 >>> ws = WordSearch(WORDS, 3, 3)
141 >>> ws.insert_southeast("cat", [0], [0])
142 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
143 [['c', None, None],
144 [None, 'a', None],
145 [None, None, 't']]
146 >>> ws.insert_southeast("at", [1, 0], [2, 1, 0])
147 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
148 [['c', None, None],
149 ['a', 'a', None],
150 [None, 't', 't']]
151 """
152 word_length = len(word)
153 # Attempt to insert the word into each row and when successful, exit
154 for row in rows:
155 # Check if there is space for the word below the row
156 if word_length + row > self.height:
157 continue
158
159 # Attempt to insert the word into each column
160 for col in cols:
161 # Check if there is space to the right of the word as well as below
162 if word_length + col > self.width:
163 continue
164
165 # Check if there are existing letters
166 # to the right of the column that will be overwritten
167 letters_diagonal_left = [
168 self.board[row + i][col + i] for i in range(word_length)
169 ]
170 if all(letter is None for letter in letters_diagonal_left):
171 # Successful, insert the word southeast
172 for i in range(word_length):
173 self.board[row + i][col + i] = word[i]
174 return
175
176 def insert_south(self, word: str, rows: list[int], cols: list[int]) -> None:
177 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected