Answer to Question #206208 in Python for siav

Question #206208

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.


  • Use the nested list to maintain the matrix.
  • Use indexing to get the operation.
  • You can iterate over the matrix or use indexing appropriately to perform appropriate operations.
1
Expert's answer
2021-06-15T15:12:31-0400
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)

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