-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnipNProd.py
More file actions
77 lines (72 loc) · 1.56 KB
/
Copy pathSnipNProd.py
File metadata and controls
77 lines (72 loc) · 1.56 KB
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
72
73
74
75
76
77
def SnipNProd(n,matrix):
'''Returns the maximum product of n adjacent elements'''
M = 0
rows = len(matrix)
cols = len(matrix[0])
Tmatrix = tuple(zip(*matrix))
Rmatrix = []
for x in matrix:
Rmatrix += [x[::-1]]
Rmatrix = tuple(Rmatrix)
print(Rmatrix)
if n > rows:
return False
if n > cols:
return False
#n horizontal
for x in matrix:
i = 0
nn = n
while i <= len(x)-n:
t = 0
m = 1
while t < n:
m *= x[i+t]
t += 1
if m > M:
M = m
i += 1
nn += 1
#n vertical
for x in Tmatrix:
i = 0
nn = n
while i <= len(x)-n:
t = 0
m = 1
while t < n:
m *= x[i+t]
t += 1
if m > M:
M = m
i += 1
nn += 1
#n diagonal forward
i = 0
while i <= rows -n:
j = 0
while j <= cols -n:
m = 1
t = 0
while t < n:
m *= matrix[i+t][j+t]
t +=1
if m > M:
M = m
j += 1
i += 1
#n diagonal back
i = 0
while i <= rows -n:
j = 0
while j <= cols -n:
m = 1
t = 0
while t < n:
m *= Rmatrix[i+t][j+t]
t +=1
if m > M:
M = m
j += 1
i += 1
return M