Tuesday, June 6, 2017

Rotate Matrix 90 Degree Anti-Clockwise

A MxN matrix is passed as the input. The program must rotate the matrix by 90 degrees in anti-clock wise direction and print the rotated matrix as the output.

Input Format: 

First line will contain the value of M. Second line will contain the value of N. Next M lines will contain the N values with each value separated by one or more space.

 Output Format:

 N lines will contain the M values with each value separated by one or more space.

Boundary Conditions: 

2 <= M <= 15 2 <= N <= 15

Example Input/Output 1: 

Input:
2
3
4 5 9
1 3 5

Output:
9 5
5 3
4 1

Solution:

row = int(input())
col = int(input())
mat = [0]*row

for i in range(row):
    mat[i] = [int(x) for x in input().strip().split()]

for i in range(col-1,-1,-1):
    for j in range(row):
        print(mat[j][i]," ", end = '')
    print('\r')
Share: