Answer to Question #206990 in Python for Naveen

Question #206990

Matrix Rotations

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. Querying: It is represented as Q K L. You need to print the value at row index K and column index L of the matrix A.

Input Next N lines contain N space-separated integers Aij Next lines contain various operations on the array.

Each operation on each line (Beginning either with R, U or Q). -1 will represent the end of input


1
Expert's answer
2021-06-14T15:29:09-0400
def rotate_matr(m:list, n:int):
	for i in range(n):
		m = [*zip(*m)]
		for j in range(len(m)):
			m[j] = list(reversed(m[j]))
	return m
matrix = []
#input matrix
matrix.append(list(map(int, input('A[0] ').split())))
for i in range(len(matrix[0])-1):
	matrix.append(list(map(int, input(f'A[{i+1}] ').split())))
R = 0
tmp_matr = matrix
while True:
	com = input('command ')
	if com == '-1':
		break
	elif com.split()[0] == 'R':
		r = int(((int(com.split()[1]))/90) % 4)
		R += r
		R %= 4
		tmp_matr = rotate_matr(tmp_matr, r)
	elif com.split()[0] == 'U':
		x, y, z = map(int, com.split()[1:4])
		matrix[x][y] = z
		tmp_matr = rotate_matr(matrix, R)
	elif com.split()[0] == 'Q':
		k, l = map(int, com.split()[1:3])
		print(tmp_matr[k][l])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS