Answer to Question #194508 in Python for MANOJ

Question #194508

Given a matrix of order M*N and a value K, write a program to rotate each ring of the matrix clockwise by K elements. If in any ring has less than or equal to K elements, then don’t rotate that ring.


1
Expert's answer
2021-05-18T02:58:05-0400
M = 3
N = 3
matrix = [[12, 23, 34],
          [45, 56, 67],
          [78, 89, 91]]
k = 0
temp = [0] * M
k = k % M
for i in range(0, N) :
    for t in range(0, M - k) :
        temp[t] = matrix[i][t]


    for j in range(M - k, M) :
        matrix[i][j - M + k] = matrix[i][j]


    for j in range(k, M) :
        matrix[i][j] = temp[j - k]
for i in range(0, N) : # display
    for j in range(0, M) :
        print ("{} " .
               format(matrix[i][j]), end = "")
    print ()

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