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.
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 ()
Comments
Leave a comment