Problem :

https://www.hackerrank.com/challenges/matrix-rotation-algo/problem


My Solution :

#!/usr/bin/env python3


def matrixRotation(matrix):
    for i in range(min(m, n)//2):
        count = 2*(m+n-2) - 8*(i)
        rotate = r % count
        while 0 < rotate:
            temp = matrix[i][i]
            matrix[i][i:n-1-i] = matrix[i][i+1:n-i]
            for j in range(i+1, m-i):
                matrix[j-1][n-1-i] = matrix[j][n-1-i]
            matrix[m-1-i][i+1:n-i] = matrix[m-1-i][i:n-1-i]
            for j in range(m-2-i, i-1, -1):
                matrix[j+1][i] = matrix[j][i]
            matrix[i+1][i] = temp
            rotate -= 1


m, n, r = map(int, input().split())
matrix = []
for _ in range(m):
    matrix.append(list(map(int, input().rstrip().split())))
matrixRotation(matrix)

for line in matrix:
    print(' '.join(map(str, line)))