-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding063.py
72 lines (58 loc) · 1.86 KB
/
dailycoding063.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
This problem was asked by Microsoft.
Given a 2D matrix of characters and a target word, write a function
that returns whether the word can be found in the matrix by going left-to-right,
or up-to-down.
For example, given the following matrix:
[['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']]
and the target word 'FOAM', you should return true, since it's the leftmost column.
Similarly, given the target word 'MASS', you should return true,
since it's the last row.
"""
def col_contains(words, target, i, j, length):
"""Checks if the target word is contained in the col
"""
return target == "".join(words[k][j] for k in range(i, length))
def row_contains(words, target, i, j, length):
"""Checks if the target word is contained in the row
"""
return target == "".join(words[i][k] for k in range(j, length))
def find_word(words, target):
"""Checks if target word exists in the words matrix
"""
length = len(target)
n, m = len(words), len(words[0])
if length > n or length > m:
return False
for i in range(length):
for j in range(length):
if words[i][j] == target[0]:
if n - i >= length and col_contains(words, target, i, j, length):
return True
if m - j >= length and row_contains(words, target, i, j, length):
return True
return False
def main():
words = [
['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']
]
tests = {
"FOAM": True,
"MASS": True,
"AB": True,
"SOQ": False,
"SAM": False,
"NBA": False
}
if all(tests[k] == find_word(words, k) for k in tests):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()