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.
Matrix Layer Rotation
Matrix Layer Rotation
+ 0 comments My Python3 solution. The rationale is that for each onion layer: 1. Get a list of coordinates on this layer (
dst
) 2. Rotate this list of coordinates to make a new list (src
) 3. For each coordinate indst
, set the value of this coordinate in the output matrix to the value of the correspondingsrc
coordinate in the original matrixdef get_layer_points( m: int, n: int, layer: int, ) -> list[tuple[int, int]]: points: list[tuple[int, int]] = [] for row in range(layer, m - layer - 1): points.append((row, layer)) for col in range(layer, n - layer - 1): points.append((m - layer - 1, col)) for row in range(m - layer - 1, layer, -1): points.append((row, n - layer - 1)) for col in range(n - layer - 1, layer, -1): points.append((layer, col)) return points def matrixRotation(matrix: list[list[int]], r: int): # Write your code here m = len(matrix) n = len(matrix[0]) output = [line.copy() for line in matrix] for layer in range(min(m, n) // 2): dst = get_layer_points(m, n, layer) steps = r % len(dst) src = dst[-steps:] + dst[:-steps] for i in range(len(dst)): output[dst[i][0]][dst[i][1]] = matrix[src[i][0]][src[i][1]] for line in output: print(*line, sep=" ")
+ 0 comments My C#. All test cases now passing. Uses 'strands' wrapping around each 'shell'. These are unwrapped, rotated and then reassembled to make the final grid.
public static void matrixRotation(List<List<int>> matrix, int r) { int shells = Math.Min(matrix.Count, matrix[0].Count) / 2; var strands = new List<List<int>>(); for (int shell = 0; shell < shells; shell++) { var strand = new List<int>(); int col; int row = shell; // East for (col=shell; col < matrix[row].Count - shell; col++) { strand.Add(matrix[row][col]); } // South col -= 1; for (row = shell+1; row < matrix.Count - shell; row++) { strand.Add(matrix[row][col]); } // West row -= 1; for (col = matrix[row].Count - shell - 2; col >= shell; col--) { strand.Add(matrix[row][col]); } // North col += 1; for (row = matrix.Count - shell - 2; row > shell; row--) { strand.Add(matrix[row][col]); } for (int rotation = 0; rotation < (r % strand.Count); rotation++) { int first = strand[0]; strand.RemoveAt(0); strand.Add(first); } strands.Add(strand); } /* foreach (var strand in strands) { foreach (var item in strand) Console.Write(item + " "); Console.WriteLine(); } Console.WriteLine(); */ var result = new int[matrix.Count,matrix[0].Count]; for (int shell = 0; shell < shells; shell++) { int col; int row = shell; int n = 0; // East for (col = shell; col < matrix[row].Count - shell; col++) { result[row, col] = strands[shell][n]; n++; } // South col -= 1; for (row = shell + 1; row < matrix.Count - shell; row++) { result[row, col] = strands[shell][n]; n++; } // West row -= 1; for (col = matrix[row].Count - shell - 2; col >= shell; col--) { result[row, col] = strands[shell][n]; n++; } // North col += 1; for (row = matrix.Count - shell - 2; row > shell; row--) { result[row, col] = strands[shell][n]; n++; } } for (int row = 0; row < matrix.Count; row++) { for (int col = 0; col < matrix[0].Count; col++) { Console.Write(result[row, col] + " "); } Console.WriteLine(); } } }
+ 0 comments Python 3
def matrixRotation(matrix, r): # Write your code here circles = min(len(matrix), len(matrix[0])) m, n = len(matrix), len(matrix[0]) _matrix = matrix.copy() for circle in range((int) (circles/2)): _m = (m - 2 * circle) - 1 _n = (n - 2 * circle) - 1 # print(_m,_n, circle) snake = [] snake.extend([matrix[i][circle] for i in range(circle, circle + _m)]) snake.extend([matrix[circle + _m][i] for i in range(circle, circle + _n)]) snake.extend([matrix[i][circle + _n] for i in range(circle + _m, circle, -1)]) snake.extend([matrix[circle][i] for i in range(circle + _n, circle, -1)]) # print(snake) _r = r % len(snake) snake = deque(snake) snake.rotate(_r) idx = 0 for i in range(circle, circle + _m): _matrix[i][circle] = snake[idx] idx += 1 for i in range(circle, circle + _n): _matrix[circle + _m][i] = snake[idx] idx += 1 for i in range(circle + _m, circle, -1): _matrix[i][circle + _n] = snake[idx] idx += 1 for i in range(circle + _n, circle, -1): _matrix[circle][i] = snake[idx] idx += 1 # print(_matrix) for i in range(m): print(" ".join([str(x) for x in _matrix[i]]))
+ 0 comments Python 3 code
def matrixRotation(matrix, r): m = len(matrix) n = len(matrix[1]) m2 = [([0] * n) for i in range(m)] number_of_rings = min(int(m/2), int(n/2)) m_rings = [] n_rings = [] #creating a list of ring indexes for i in range(number_of_rings): elements_in_a_ring = 2*n + 2*(m-2) m_arr = [] n_arr = [] for j in range(n): m_arr.append(i) n_arr.append(n-j-1+i) for j in range(m-1): m_arr.append(j+1+i) n_arr.append(i) for j in range(n-1): m_arr.append(m-1+i) n_arr.append(j+1+i) for j in range(m-2): m_arr.append(m-j-2+i) n_arr.append(n-1+i) #Rotating index ring by r m_arr = [m_arr[(i-r) % len(m_arr)] for i, x in enumerate(m_arr)] n_arr = [n_arr[(i-r) % len(n_arr)] for i, x in enumerate(n_arr)] m_rings.append(m_arr) n_rings.append(n_arr) m -= 2 n -= 2 # print(f"m rings after: {m_rings}") # print(f"n rings after : {n_rings}") m = len(matrix) n = len(matrix[1]) matr2 = [([0] * n) for i in range(m)] #assign the data to the new matrix (matr2), from the data matrix (matrix) using the rotated data indexes for i in range(number_of_rings): elements_in_a_ring = 2*n + 2*(m-2) for j in range(n): matr2[i][n-j-1+i] = matrix[m_rings[i][j]][n_rings[i][j]] for j in range(m-1): matr2[j+1+i][i] = matrix[m_rings[i][j+n]][n_rings[i][j+n]] for j in range(n-1): matr2[m-1+i][j+1+i] = matrix[m_rings[i][m+n-1+j]][n_rings[i][m+n-1+j]] for j in range(m-2): m_arr.append(m-j-2+i) n_arr.append(n-1+i) matr2[m-j-2+i][n-1+i] = matrix[m_rings[i][m+(2*n)-2+j]][n_rings[i][m+(2*n)-2+j]] m -= 2 n -= 2 # print(f"m matrix 1: {matrix}") # print(f"m matrix 2: {matr2}") m = len(matrix) for i in range(m): print(*matr2[i])
+ 0 comments Python 3.
Eventhough this solution is short, it takes too many ierations to complete the totations. So some test cases will exceed the time limit. I also commentd anothor optimized solution, that works for every test case.
def matrixRotation(matrix, r): m = len(matrix) n = len(matrix[1]) m2 = [([0]*n) for i in range(m)] for turns in range(r): m2 = [([0] * n) for i in range(m)] for i in range(min(int(m/2), int(n/2))): m2[i][n-i-1] = matrix[i+1][n-i-1] m2[m-i-1][i] = matrix[m-i-2][i] for j in range(n-(2*i)-1): m2[i][i+j] = matrix[i][i+j+1] m2[m-i-1][i+j+1] = matrix[m-i-1][i+j] for k in range(m-(2*i)-1): m2[k+i+1][i] = matrix[k+i][i] m2[k+i][n-i-1] = matrix[k+i+1][n-i-1] matrix = m2 for i in range(m): print(*matrix[i])
Load more conversations
Sort 706 Discussions, By:
Please Login in order to post a comment