Answer to Question #165551 in Python for prathyusha

Question #165551

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.


1
Expert's answer
2021-02-23T04:02:44-0500
def Rotate_90_CW(InArr,Angle):
    Ang = int(Angle/90)
    for n in range(0,Ang):
        M = len(InArr[0])
        for i in range(M // 2):
            for j in range(i, M - i - 1):
                temp = InArr[i][j]
                InArr[i][j] = InArr[M - 1 - j][i]
                InArr[M - 1 - j][i] = InArr[M - 1 - i][M - 1 - j]
                InArr[M - 1 - i][M - 1 - j] = InArr[j][M - 1 - i]
                InArr[j][M - 1 - i] = temp
    return(InArr)


arr = [ [ 1, 2, 3, 4 ],
          [ 5, 6, 7, 8 ],
          [ 9, 10, 11, 12 ],
          [ 13, 14, 15, 16 ] ]


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


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


Python Output





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