• + 0 comments

    Python solution using dictionary:

    def matrixRotation(matrix, r):
        m = len(matrix)
        n = len(matrix[0])
        d = dict() # storing matrix coordinates and their values
        for i in range(min(m, n) // 2):
            # storing the coordinates for each loop or layer of the matrix
            loop = [(i, j) for j in range(i, n - i)] # top side
            loop += [(j, n - i - 1) for j in range(i + 1, m - i)] # right side
            loop += [(m - i - 1, j) for j in range(n - i - 2, i - 1 , -1)] # bottom side
            loop += [(j, i) for j in range(m - i - 2, i , -1)] # left side
            # listing the values of each loop of the matrix, then shift to mimic the effect of rotation
            loop_values = [matrix[r][c] for r, c in loop]
            rotated_values = loop_values[r % len(loop_values):] + loop_values[: r % len(loop_values)]
            # storing the new values of every position of the loop to respective coordinates
            for k in range(len(loop)):
                d[loop[k]] = rotated_values[k]
        # print out the resultant matrix
        for r in range(m):
            line = [d[(r, c)] for c in range(n)]
            print(*line)