Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A rotate function that rotates a two-dimensional array (a matrix) either clockwise or anti-clockwise by 90 or 180 degrees with multiple times and returns the rotated array.
- The function accepts two parameters: an array, and a string specifying the direction or rotation. The direction will be either "clockwise" or "counter-clockwise".
- ==> It can be square matrix or non-square matrix
- ==> It can be single row or column
- Here is examples of how your function will be used:
- [[1, 2, 3], | counter-clockwise
- [4, 5, 6], |
- | ==========>>>>>> [[3, 6, 9, 12], [2, 5, 8, 11], [1, 4, 7, 10]]
- [7, 8, 9], |
- [10,11,12]] |
- matrix=[[1, 2, 3], clockwise
- [4, 5, 6], =======>>>> [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
- [7, 8, 9]]
- =====================================================================================================================================
- @CODE
- def rotate(matrix,direction):
- if direction == "clockwise":
- return [list(i) for i in zip(*matrix[::-1])]
- elif direction == "counter-clockwise":
- cn_cl = [list(i[::-1]) for i in zip(*matrix[::-1])]
- return cn_cl[::-1]
Add Comment
Please, Sign In to add comment