Matrix Rotations
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.
Sample Input 2
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
Sample Output 2
5
8
8
i want exact sample outputs
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))
Comments
Leave a comment