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

Method insert_northeast

other/word_search.py:68–104  ·  view source on GitHub ↗

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

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

Source from the content-addressed store, hash-verified

66 return
67
68 def insert_northeast(self, word: str, rows: list[int], cols: list[int]) -> None:
69 """
70 >>> ws = WordSearch(WORDS, 3, 3)
71 >>> ws.insert_northeast("cat", [2], [0])
72 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
73 [[None, None, 't'],
74 [None, 'a', None],
75 ['c', None, None]]
76 >>> ws.insert_northeast("at", [0, 1], [2, 1, 0])
77 >>> ws.board # doctest: +NORMALIZE_WHITESPACE
78 [[None, 't', 't'],
79 ['a', 'a', None],
80 ['c', None, None]]
81 """
82 word_length = len(word)
83 # Attempt to insert the word into each row and when successful, exit
84 for row in rows:
85 # Check if there is space for the word above the row
86 if word_length > row + 1:
87 continue
88
89 # Attempt to insert the word into each column
90 for col in cols:
91 # Check if there is space to the right of the word as well as above
92 if word_length + col > self.width:
93 continue
94
95 # Check if there are existing letters
96 # to the right of the column that will be overwritten
97 letters_diagonal_left = [
98 self.board[row - i][col + i] for i in range(word_length)
99 ]
100 if all(letter is None for letter in letters_diagonal_left):
101 # Successful, insert the word northeast
102 for i in range(word_length):
103 self.board[row - i][col + i] = word[i]
104 return
105
106 def insert_east(self, word: str, rows: list[int], cols: list[int]) -> None:
107 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected