Lecture Notes on 19 Apr 2017
def main():
# open file for reading
in_file = open ("./grid.txt", "r")
# read the dimension of the grid
dim = in_file.readline()
dim = dim.strip()
dim = int (dim)
# create an empty grid
grid = []
# populate the grid
for i in range (dim):
line = in_file.readline()
line = line.strip()
row = line.split()
for j in range (dim):
row[j] = int (row[j])
grid.append (row)
# print the grid
# print (grid)
# close the file
in_file.close()
# read and multiply in blocks of four along rows
for row in grid:
for i in range (dim - 3):
prod = 1
for j in range (i, i + 4):
prod = prod * row[j]
print (prod, end = " ")
print ()
# read each column in blocks of four
for j in range (dim):
for i in range (dim - 3):
for k in range (i, i + 4):
print (grid[k][j], end = " ")
print (end = " ")
print ()
# go along all diagonals L to R in blocks of 4
for i in range (dim - 3):
for j in range (dim - 3):
for k in range (4):
print (grid[i + k][j + k], end = " ")
print (end = " ")
print ()
# go along all diagonals R to L in blocks of 4
for i in range (dim - 3):
for j in range (3, dim):
for k in range (4):
print (grid[i + k][j - k], end = " ")
print (end = " ")
print ()
main()