Answer to Question #308526 in Python for satyasrini

Question #308526

a square matrix A of dimensions NxN. You need to apply the below given 3 operations on matrix A.

Rotation: It represented as R S where S is integer in {90, 180, 270, 360, 450, .} which denotes the number of degrees to rotate. You need to rotate matrix A by angle S in the clockwise 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.

Query: It is represented as Q K L. need to print the value at row index K and column index L of the matrix A.

Next N lines contain N space-separated integers Aij (i - index row, j - index of column)


-1 will represent the end of input.




S Input:


2

5 6

7 8

R 90

Q 0 1

R 270

Q 1 1

R 180

U 0 0 4

Q 0 0

-1

op:

5

8

8












1
Expert's answer
2022-03-09T07:53:54-0500
def Query(Q,K,L):
    return(Q[K][L])


def UpdateVal(U,X,Y,Z):
    U[X][Y]=Z
    return(U)


#Array matrix rotation 
def Rotate(Arr,Angle):
    A = int(Angle/90)
    for n in range(0,A):
        M = len(Arr[0])
        for i in range(M // 2):
            for j in range(i, M - i - 1):
                t = Arr[i][j]
                Arr[i][j] = Arr[M - 1 - j][i]
                Arr[M - 1 - j][i] = Arr[M - 1 - i][M - 1 - j]
                Arr[M - 1 - i][M - 1 - j] = Arr[j][M - 1 - i]
                Arr[j][M - 1 - i] = t
    return(Arr)


arr =   [ [ 1, 2, 3, 4 ],
          [ 5, 0, 7, 8 ],
          [ 9, 10, 11, 12 ],
          [ 103, 104, 105, 106 ] ]


S=[90,180,270,360]
print("Input Array:")
N = len(arr[0])
for i in range(0,N):
    for j in range(0,N):
        print(arr[i][j],end=" ")
    print()


for r in range (0,len(S)):
    NewArr = Rotate(arr,S[r])
    print("Rotated Array at an angle = %d:"%S[r])
    for i in range(0,N):
        for j in range(0,N):
            print(NewArr[i][j],end=" ")
        print()
    print("\n")




print("\Input Array:")
N = len(arr[0])
for i in range(0,N):
    for j in range(0,N):
        print(arr[i][j],end=" ")
    print()
arr = UpdateVal(arr,2,1,20)
print("\nArray after updation: ")
for r in range(0,N):
    for j in range(0,N):
        print(arr[r][j],end=" ")
    print()
for r in range (0,len(S)):
    NewArr = Rotate(arr,S[r])
    print("Rotated Array at an angle = %d:"%S[r])
    for i in range(0,N):
        for j in range(0,N):
            print(NewArr[i][j],end=" ")
        print()
    print("\n")


print("\nQuerying Result:")
print(Query(arr,2,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
New on Blog
APPROVED BY CLIENTS