Answer to Question #235058 in Python for dnyanesh

Question #235058

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.



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-09-09T05:35:09-0400
def rotation(x, s):
    n = len(x)
    rot = (s // 90) % 4
    for r in range(rot):
        temp = []
        for i in range(n):
            column = [row[i] for row in x]
            column.reverse()
            temp.append(column)
        x = temp
    return x


N = int(input('Input N: '))
orig_matrix = []
for i in range(N):
    orig_matrix.append([])
    for j in range(N):
        orig_matrix[i].append(int(input(f'Input num in {i} {j}: ')))
rot = 0
matrix = orig_matrix.copy()
while True:
    line = input().split()
    if line[0] == '-1':
        break
    elif line[0] == 'R':
        rot += int(line[1])
        matrix = rotation(matrix, int(line[1]))
    elif line[0] == 'U':
        orig_matrix[int(line[1])][int(line[2])] = int(line[3])
        matrix = rotation(orig_matrix, rot)
    elif line[0] == 'Q':
        print(matrix[int(line[1])][int(line[2])])

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
APPROVED BY CLIENTS