SriHarsha
SriHarsha

Reputation: 19

Rotations Matrix

You are given a square matrix A of dimensions NxN. You need to apply the below given 3 operations on 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 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.

This was my question. Here is what I have done so far:

def ReadMatrix():
    matrix = []
    for i in range(int(input())):
        row = [int(j) for j in input().split()]
        matrix.append(row) 
    return matrix

def RotateMatrix(matrix, degrees):
    n = len(matrix[0])
    rotations = (degrees // 90) % 4
    for r in range(rotations):
        temp_matrix = []
        for i in range(n):
            column = [row[i] for row in matrix]
            column.reverse()
            temp_matrix.append(column)
        matrix = temp_matrix
    return matrix

matrix = ReadMatrix()
rotation = 0

while True:
    line = input().split()
    if line[0] == "-1":
        break;
    elif line[0] == "R":
        rotation += int(line[1])
        matrix = RotateMatrix(matrix, int(line[1]))
    elif line[0] == "U":
         matrix[int(line[1])][int(line[2])] = int(line[3])
         matrix = RotateMatrix(matrix, rotation)
    elif line[0] == "Q":
        print(matrix[int(line[1])][int(line[2])]) 
    else:
        print(line[0])
        exit(1)

I am getting correct output for this input:

2
1 2
3 4
R 90
Q 0 0
Q 0 1
R 90
Q 0 0
U 0 0 6
Q 1 1
-1

But I am not getting correct output for this 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

The output I am getting for the second input is 5 8 5 but the correct output is 5 8 8.

Upvotes: 1

Views: 4260

Answers (3)

 a = int(input())
 matrix = []
 rotation = 0

for i in range(a):
    row = [int(j) for j in input().split()]
    matrix.append(row) 
orig_matrix = matrix.copy() 

def RotateMatrix(matrix, degrees):
    n = len(matrix[0])
    rotations = (degrees // 90) % 4

    for r in range(rotations):
        temp_matrix = []

        for i in range(n):
            column = [row[i] for row in matrix]
            column.reverse()
            temp_matrix.append(column)

        matrix = temp_matrix
    return matrix
    
while True:
    line = input().split()
    if line[0] == "-1":
        break;

    elif line[0] == "R":
        rotation += int(line[1])
        matrix = RotateMatrix(matrix, int(line[1]))

    elif line[0] == "U":
         orig_matrix[int(line[1])][int(line[2])] = int(line[3])
         matrix = RotateMatrix(orig_matrix, rotation)

    elif line[0] == "Q":
        print(matrix[int(line[1])][int(line[2])])

    else:
        print("Error: unexpected command '" + line[0] + "'")
        exit(1)

your code is fine. but miss this condition
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*.**

Upvotes: 0

Dilip Kumar
Dilip Kumar

Reputation: 1

exit=1
ar=[]
pr=[]
up=0
pu=0
def array_rot(rang, ar):
    for k in range(rang):
            for i in range(a//2):
                c=''
                for j in range(i, a-i-1):
                    temp=ar[i][j]
                    ar[i][j]=ar[a-1-j][i]
                    ar[a-1-j][i]=ar[a-1-i][a-1-j]
                    ar[a-1-i][a-1-j]=ar[j][a-1-i]
                    ar[j][a-1-i]=temp
    return ar
for i in range(a):
    inp=input().split()
    ar.append(inp)
while(exit!=-1):
    q=input()
    r=q.split()
    if(q[0]=='R'):
        rang=int(r[1])//90
        up=up+rang
        pu=rang+pu
        ar=array_rot(rang, ar)
    elif(q[0]=='U'):
        ran=0
        while(up%4!=0):
            ran=ran+1 
            up=up+1
        ar=array_rot(ran, ar)
        ar[int(r[1])][int(r[2])]=r[3]
        ar=array_rot(pu,pr)
        pu=0
    elif(q[0]=='Q'):
        print(ar[int(r[1])][int(r[2])])
    elif(r[0]=='-1'):
        exit=-1

Upvotes: 0

AirSquid
AirSquid

Reputation: 11883

You are very close. Of course you knew that. :)

The problem you are having is that you are not explicitly following the instruction for the Update action. It says you should apply the update to the original matrix and then re-rotate it. You are applying the update to the already rotated matrix and then re-rotating it.

So. How do we fix? We need to maintain a copy of the original matrix for such purposes and then use that as the basis of our Update command. Python has a built in copy() command which will work fine in this case because the thing you are copying are lists of integer types (not reference variables).

Consider capturing your original matrix like this:

matrix = ReadMatrix()
orig_matrix = matrix.copy()    

Then you can update that as needed and pass in that updated matrix into your re-rotation operation.

If you want to watch the play-by-play to verify what is going on, I suggest adding a little print statement to the end of your loop that you can comment out when satisfied that all is good...

...
else:
    print(line[0])
    sys.exit(1)
print('matrix:', matrix)
print('orig  :', orig_matrix)

Upvotes: 2

Related Questions