We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defmatrixRotation(matrix,r):m=len(matrix)n=len(matrix[0])d=dict()#storingmatrixcoordinatesandtheirvaluesforiinrange(min(m,n)// 2):# storing the coordinates for each loop or layer of the matrixloop=[(i,j)forjinrange(i,n-i)]#topsideloop+=[(j,n-i-1)forjinrange(i+1,m-i)]#rightsideloop+=[(m-i-1,j)forjinrange(n-i-2,i-1,-1)]#bottomsideloop+=[(j,i)forjinrange(m-i-2,i,-1)]#leftside# listing the values of each loop of the matrix, then shift to mimic the effect of rotationloop_values=[matrix[r][c]forr,cinloop]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 coordinatesforkinrange(len(loop)):d[loop[k]]=rotated_values[k]# print out the resultant matrixforrinrange(m):line=[d[(r,c)]forcinrange(n)]print(*line)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
Python solution using dictionary: