-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_063.py
51 lines (35 loc) · 1.36 KB
/
problem_063.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def get_row_word(matrix, word_len, rows, x, y):
row_chars = list()
for i in range(word_len):
row_chars.append(matrix[x + i][y])
return "".join(row_chars)
def get_col_word(matrix, word_len, cols, x, y):
return "".join(matrix[x][y:y + word_len])
def word_checker(matrix, word, word_len, rows, cols, x, y):
if x >= rows or y >= cols:
return False
row_word, col_word = None, None
if x + word_len <= rows and y < cols:
row_word = get_row_word(matrix, word_len, rows, x, y)
if y + word_len <= cols and x < rows:
col_word = get_col_word(matrix, word_len, cols, x, y)
if row_word == word or col_word == word:
return True
check_1 = word_checker(matrix, word, word_len, rows, cols, x + 1, y) \
if col_word else None
check_2 = word_checker(matrix, word, word_len, rows, cols, x, y + 1) \
if row_word else None
return check_1 or check_2
def word_exists(matrix, word):
rows = len(matrix)
cols = len(matrix[0])
word_len = len(word)
return word_checker(matrix, word, word_len, rows, cols, 0, 0)
matrix = [['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']]
assert not word_exists(matrix, "FOAMS")
assert word_exists(matrix, "FOAM")
assert word_exists(matrix, "MASS")
assert not word_exists(matrix, "FORM")