PE11
square = [ s.split() for s in open("square.txt").readlines()]
input_li = []
for i in range(0,len(square)):
tmp = []
for j in range(0,len(square[i])):
tmp.append(int(square[i][j]))
input_li.append(tmp)
# max product of 4 adjacent number left -> right
def max_right(input_li):
result = {}
max_down = 0
#calculate product of 4 adjacent number left -> right
for row in range(0,len(input_li)):
for col in range(0,len(input_li[row])-3):
tmp_p = (input_li[row][col] * input_li[row][col+1] * input_li[row][col+2] * input_li[row][col+3])
if (max_down < tmp_p):
result[0] = (row,col,tmp_p)
max_down = tmp_p
return result
# max product of 4 adjacent number right -> left
def max_left(input_li):
result = {}
max_left = 0
#calculate product of 4 adjacent number left -> right
for row in range(0,len(input_li)):
for col in range(len(input_li[row])-1,3,-1):
tmp_p = (input_li[row][col] * input_li[row][col-1] * input_li[row][col-2] * input_li[row][col-3])
if (max_left < tmp_p):
result[0] = (row,col,tmp_p)
max_left = tmp_p
return result
# max product of 4 adjacent number down
def max_down(input_li):
result = {}
max_down = 0
#calculate product of 4 adjacent number left -> right
for row in range(0,len(input_li)-3):
for col in range(0,len(input_li[row])):
tmp_p = (input_li[row][col] * input_li[row+1][col] * input_li[row+2][col] * input_li[row+3][col])
if (max_down < tmp_p):
result[0] = (row,col,tmp_p)
max_down = tmp_p
return result
# max product of 4 adjacent number up
def max_up(input_li):
result = {}
max_up = 0
#calculate product of 4 adjacent number left -> right
for row in range(len(input_li)-1,3,-1):
for col in range(0,len(input_li[row])):
tmp_p = (input_li[row][col] * input_li[row-1][col] * input_li[row-2][col] * input_li[row-3][col])
if (max_up < tmp_p):
result[0] = (row,col,tmp_p)
max_up = tmp_p
return result
# max product of 4 adjacent number diagonal
def max_d(input_li):
result = {}
max_d = 0
#calculate product of 4 adjacent number left -> right
for row in range(0,len(input_li)-3):
for col in range(0,len(input_li[row])-3):
tmp_p = (input_li[row][col] * input_li[row+1][col+1] * input_li[row+2][col+2] * input_li[row+3][col+3])
if (max_d < tmp_p):
result[0] = (row,col,tmp_p)
max_d = tmp_p
return result
# max product of 4 adjacent number diagonal - down to up - right to left
def max_d2(input_li):
result = {}
max_d = 0
#calculate product of 4 adjacent number left -> right
for row in range(len(input_li)-1,3,-1):
for col in range(len(input_li[row])-1,3,-1):
tmp_p = (input_li[row][col] * input_li[row-1][col-1] * input_li[row-2][col-2] * input_li[row-3][col-3])
if (max_d < tmp_p):
result[0] = (row,col,input_li[row][col], input_li[row-1][col-1] ,input_li[row-2][col-2] ,input_li[row-3][col-3],tmp_p)
max_d = tmp_p
return result
# max product of 4 adjacent number diagonal - down to up - left to right
def max_d3(input_li):
result = {}
max_d = 0
#calculate product of 4 adjacent number left -> right
for row in range(len(input_li)-1,3,-1):
for col in range(0,len(input_li[row])-3):
tmp_p = (input_li[row][col] * input_li[row-1][col+1] * input_li[row-2][col+2] * input_li[row-3][col+3])
if (max_d < tmp_p):
result[0] = (row,col,input_li[row][col], input_li[row-1][col+1] ,input_li[row-2][col+2] ,input_li[row-3][col+3],tmp_p)
max_d = tmp_p
return result
max_right= max_right(input_li)
max_left = max_left(input_li)
max_down = max_down(input_li)
max_up = max_up(input_li)
max_d = max_d(input_li)
max_d2 = max_d2(input_li)
max_d3 = max_d3(input_li)
print(max_right)
print(max_left)
print(max_down)
print(max_up)
print(max_d)
print(max_d2)
print(max_d3)INFO