Rotate Matrix
Advanced
In this challenge, you are going to write a function that rotates a square matrix to the right by 90 degrees. Imagine you have a grid like a chessboard, and you rotate it as if turning a page.
A square matrix is simply a list of lists, where each sub-list represents a row of the matrix. All rows have the same number of elements as the number of rows, forming a perfect square.
Here's how a rotation works:
Original Matrix:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
After rotating 90 degrees to the right:
[
[7, 4, 1],
[8, 5, 2],
[9, 6, 3]
]
Your task is to rotate any given square matrix to the right by 90 degrees!
Examples
rotate_matrix([ [1, 2], [3, 4] ]) # Returns: # [ # [3, 1], # [4, 2] # ] rotate_matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Returns: # [ # [7, 4, 1], # [8, 5, 2], # [9, 6, 3] # ]
Remember, the matrix will always be "n by n" where n is the number of rows or columns, so you don't need to worry about any other dimensions! Now it's your turn to code! Happy coding!
Inputs
Arg 1:
[[1,2],[3,4]]Expected Output
[[3, 1], [4, 2]]Actual Output