• + 0 comments

    Best I could think of

    def matrixRotation(matrix, r):
        n = len(matrix)
        m = len(matrix[0])
        lowerit = math.floor(min(n, m)/2)
        
        n -= 1
        m -= 1
        for i in range(lowerit):
            arr = []
            endl = n - i
            endr = m - i
            for j in range(i, endl+1):
                arr.append(matrix[j][i])
            for j in range(i + 1, endr+1):
                arr.append(matrix[endl][j])
            for j in range(endl-1, i-1, -1):
                arr.append(matrix[j][endr])
            for j in range(endr-1, i, -1):
                arr.append(matrix[i][j])
                
            bigrcheck =  r % len(arr)
            
            nwarr = arr[len(arr)-bigrcheck:] + arr[:len(arr)-bigrcheck] 
            
            counter = 0
            for j in range(i, endl+1):
                matrix[j][i] = nwarr[counter]
                counter += 1
            for j in range(i + 1, endr+1):
                matrix[endl][j] = nwarr[counter]
                counter += 1
            for j in range(endl-1, i-1, -1):
                matrix[j][endr] = nwarr[counter]
                counter += 1
            for j in range(endr-1, i, -1):
                matrix[i][j] = nwarr[counter]
                counter += 1
        for line in matrix:
            print(*line)