You are given a square matrix A of dimensions NxN. You need to apply the below given 3 operations on the matrix A.
Rotation: It is represented as R S where S is an integer in {90, 180, 270, 360, 450, ...} which denotes the number of degrees to rotate. You need to rotate the matrix A by angle S in the clockwise direction. The angle of rotation(S) will always be in multiples of 90 degrees.
Update: It is represented as U X Y Z. In initial matrix A (as given in input), you need to update the element at row index X and column index Y with value Z.
After the update, all the previous rotation operations have to be applied to the updated initial matrix.
def rotate(matr, a):
a = int(a / 90)
a = int(a % 4)
for i in range(a):
matr = [*zip(*matr)]
for j in range(len(matr)):
matr[j] = list(reversed(matr[j]))
return matr
def update(matr, x, y, z):
matr[x][y] = z
return matr
def print_el(matr, x, y):
print(matr[x][y])
matr = []
N = int(input('matrix dimention N = '))
for i in range(N):
matr.append(list(map(int, input().split())))
R = 0
while True:
operation = input('command ').split()
if operation[0] == '-1':
break
elif operation[0] == 'R':
R += int(operation[1])
elif operation[0] == 'U':
x, y, z = map(int, operation[1:4])
matr = update(matr, x, y, z)
elif operation[0] == 'Q':
x, y = map(int, operation[1:3])
print_el(rotate(matr, R), x, y)
Comments
Leave a comment