Last active
July 25, 2022 15:37
-
-
Save corehello/e3c48923d71059c13f3cc6d94e65dd70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
def generateNdimensionArray(n): | |
""" | |
this function generate a n-dimension array like following | |
[[1, 2, 3, 4, 5], | |
[6, 7, 8, 9, 10], | |
[11, 12, 13, 14, 15], | |
[16, 17, 18, 19, 20], | |
[21, 22, 23, 24, 25]] | |
""" | |
return [[i+j*n +1 for i in range(n)] for j in range(n)] | |
def rotate90_array(a, d): | |
""" | |
this function rotate a n-dimension array like following | |
Original array is: | |
[[1, 2, 3, 4, 5], | |
[6, 7, 8, 9, 10], | |
[11, 12, 13, 14, 15], | |
[16, 17, 18, 19, 20], | |
[21, 22, 23, 24, 25]] | |
Rotated 90 clockwise array is: | |
[[21, 16, 11, 6, 1], | |
[22, 17, 12, 7, 2], | |
[23, 18, 13, 8, 3], | |
[24, 19, 14, 9, 4], | |
[25, 20, 15, 10, 5]] | |
""" | |
for i in range(0, int(d/2)): | |
for j in range(i, d-i-1): | |
a[i][j], a[d-1-j][i], a[d-1-i][d-1-j], a[j][d-1-i] = \ | |
a[d-1-j][i], a[d-1-i][d-1-j], a[j][d-1-i], a[i][j] | |
if __name__ == '__main__': | |
n=5 | |
array = generateNdimensionArray(n) | |
print("Original array is:") | |
pprint(array) | |
rotate90_array(array, len(array)) | |
from pprint import pprint | |
print("Rotated 90 clockwise array is:") | |
pprint(array) |
Author
corehello
commented
Jul 25, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment