The rotate-matrix is an interesting new python module, which allows conversion of a matrix(2-D array) into either clockwise rotation or anti-clockwise rotation. It at present consists of only two methods. You can loop through these methods for ‘n’ number of rotations.
Installation
This module doesn’t come in-built with python but ca be installed using the following command:
pip install rotate-matrix
Functions
1) clockwise(): As obviously the name suggests, this function is used to rotate the matrix clockwise.
Syntax: clockwise(matrix)
Parameter: Takes matrix as parameter which is of type list.
Return Value: passes the clock-wised rotated version of the matrix
Example:
Python3
import rotate_matrix mat = [[ 5 , 2 , 6 ], [ 8 , 2 , 9 ], [ 3 , 6 , 7 ], [ 3 , 6 , 2 ]] print (rotate_matrix.clockwise(mat)) |
Output:
[(6, 9, 7, 2), (2, 2, 6, 6), (5, 8, 3, 3)]
2) anti_clockwise(): This function of this module rotates the given matrix anti-clockwise.
Syntax: anti_clockwise(matrix)
Parameter: Takes matrix as parameter which is of type list
Return Value: passes the anti-clockwise rotated version of the matrix
Example:
Python3
import rotate_matrix mat = [[ 5 , 2 , 6 ], [ 8 , 2 , 9 ], [ 3 , 6 , 7 ], [ 3 , 6 , 2 ]] print (rotate_matrix.anti_clockwise(mat)) |
Output:
[(3, 3, 8, 5), (6, 6, 2, 2), (2, 7, 9, 6)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.