Answer to Question #206019 in Python for chavali anand babu

Question #206019
Rotate Matrix Rings
Given a matrix of order M*N and a value K, write a program to rotate each ring of th
The next M lines will contain N space-separated integers.
The next line will contain an integer, denoting K.
Output..The output should be M*N matrix by rotating the matrix by K elements.
Explanation...If the given K is 3. Rotate each ring of the matrix by 3 elements.
In the above matrix, the elements (1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5) is a ring, similarly, the elements (6, 7, 11, 10) will make a ring.
Therefore, by rotating each ring in clockwise direction by 3 elements will give (13, 9, 5, 1, 2, 3, 4, 8, 12, 16, 15, 14) and (10, 6, 7, 11). So the output should be 
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
Sample Input 1
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
Sample Output 1
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
Sample Input 2
3 4
1 2 3 4
10 11 12 5
9 8 7 6
2
Sample Output 2
9 10 1 2
8 11 12 3
7 6 5 4

sir give full answer to all test cases will be passed original output



1
Expert's answer
2021-06-13T00:03:35-0400
def rotate1(matrix, n):
    a = len(matrix)
    n = int(a)
    b = int(n/2)
    
    for row in range(0, b):
        for col in range(row, (n- row - 1)):
            temp = matrix[row][col]
            matrix[row][col] = matrix[n - 1 - col][row]       
            matrix[n - 1 - col][row] = matrix[n - 1 - row][n- 1 - col]   
            matrix[n - 1 - row][n - 1 - col] = matrix[col][n - 1 - row]     
            matrix[col][n - 1 - row] = temp   
    return matrix

matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]]
n = len(matrix)
array = rotate1(matrix, n)
for col in array:
    print(col, sep=" ")
          


    

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS