TCS Codevita 2014 Matrix Rotations question with solution

                               Problem: Matrix Rotation

You are given a square matrix of dimension N. Let this matrix be called A. Your task is to rotate A in clockwise direction byS degrees, where S is angle of rotation. On the matrix, there will be 3 types of operations viz. 
  1. Rotation
    Rotate the matrix A by angle S, presented as input in form of A S 
  2. Querying
    Query the element at row K and column L, presented as input in form of Q K L
  3. Updation
    Update the element at row X and column Y with value Z,  presented as input in form of U X Y Z
Print the output of individual operations as depicted in Output Specification

Input Format:

Input will consist of three parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N)
3. Various operations on the matrix, one operation on each line. (Beginning either with A, Q or U)

-1 will represent end of input.
Note:
  • Angle of rotation will always be multiples of 90 degrees only.
  • All Update operations happen only on the initial matrix. After update all the previous rotations have to be applied on the updated matrix

Output Format:

For each Query operation print the element present at K-L location of the matrix in its current state.

Constraints:

1<=N<=1000
1<=Aij<=1000
0<=S<=160000
1<=K, L<=N
1<=Q<=100000


Sample Input and Output

SNo.InputOutput
1
2
1 2
3 4
A 90
Q 1 1
Q 1 2
A 90
Q 1 1
U 1 1 6
Q 2 2
-1

3
1
4
6
Solution:

import numpy as np

def rotate(x, n):
    length = len(n)
    # matrix transpose
    for i in range(length):
        for j in range(i, length):
            n[i][j], n[j][i] = n[j][i], n[i][j]


    # rotation clockwise
    for i in range(length):
        for j in range(int(length / 2)):
            n[i][j], n[i][length - 1 - j] = n[i][length - 1 - j], n[i][j]

    return n

size = int(input("enter size of matrix:"))
if 1<=size<=1000:
    print("Enter Matrix Values:")
    m = list(map(str, input().split(" ")))
    n = np.array(m,int).reshape(size, size)
    initial = np.array(m,int).reshape(size, size)
    print(initial)
    for i in range(len(n)):
        for j in range(len(n)):
            if 1<=n[i][j]<=1000:
                continue
            else:
                print("Matrix Values Is always >=1 and <=1000.")
                exit(0)

    angle = 0
    while(True):
        c = input("Enter option:")
        if c == 'A':
            x = int(input("Enter angle for matrix rotation:"))
            num = int((x / 90) % 4)
            while num>0:
                n = rotate(angle, n)
                num-=1
            angle += x
        elif c == 'Q':
            x = int(input("enter row:"))
            y = int(input("enter column:"))
            if x>size or y>size:
                print("Given Values is greater than Matrix size")
            print(n[x-1][y-1])
        elif c == 'U':
            x = int(input("enter row:"))
            y = int(input("enter column:"))
            z = int(input("Enter New Value:"))
            n= initial
            n[x - 1][y - 1] = z
            num = int((angle / 90)%4)

            while num>0:
                n = rotate(angle, n)
                num-=1
            print(n)
        elif c == '-1':
            break
        else:
            print("Wrong Input!!")

Comments

Unknown said…
This Is very helpful for me..Thanxx
Anonymous said…
Nice One