Answer to Question #197759 in Python for Meghana

Question #197759

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





1
Expert's answer
2021-05-24T04:11:47-0400
def Querying(Q,K,L):
    return(Q[K][L])


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


#Array matrix rotation 
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")




print("\nOriginal Array:")
N = len(arr[0])
for i in range(0,N):
    for j in range(0,N):
        print(arr[i][j],end=" ")
    print()
arr = Update(arr,1,1,20)
print("\nArray after updation: ")
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")
print("\nQuerying Result:")
print(Querying(arr,1,2))


Python Output:

Original Array:

1 2 3 4 

5 6 7 8 

9 10 11 12 

13 14 15 16 

Rotated Array at angle = 90:

13 9 5 1 

14 10 6 2 

15 11 7 3 

16 12 8 4 



Rotated Array at angle = 180:

4 8 12 16 

3 7 11 15 

2 6 10 14 

1 5 9 13 



Rotated Array at angle = 270:

16 15 14 13 

12 11 10 9 

8 7 6 5 

4 3 2 1 



Rotated Array at angle = 360:

16 15 14 13 

12 11 10 9 

8 7 6 5 

4 3 2 1 



Rotated Array at angle = 450:

4 8 12 16 

3 7 11 15 

2 6 10 14 

1 5 9 13 




Original Array:

4 8 12 16 

3 7 11 15 

2 6 10 14 

1 5 9 13 


Array after updation: 

4 8 12 16 

3 20 11 15 

2 6 10 14 

1 5 9 13 

Rotated Array at angle = 90:

1 2 3 4 

5 6 20 8 

9 10 11 12 

13 14 15 16 



Rotated Array at angle = 180:

16 15 14 13 

12 11 10 9 

8 20 6 5 

4 3 2 1 



Rotated Array at angle = 270:

13 9 5 1 

14 10 6 2 

15 11 20 3 

16 12 8 4 



Rotated Array at angle = 360:

13 9 5 1 

14 10 6 2 

15 11 20 3 

16 12 8 4 



Rotated Array at angle = 450:

16 15 14 13 

12 11 10 9 

8 20 6 5 

4 3 2 1 




Querying Result:

10

>>> 



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