Created
September 4, 2014 15:15
-
-
Save craigpalermo/ba10164f7426947a6240 to your computer and use it in GitHub Desktop.
Interview question, rotate elements in 2D array 90 degrees clockwise
This file contains hidden or 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
n = 5 | |
arr = [] | |
# populate array | |
for i in range(0, n): | |
arr.append([]) | |
for j in range(0, n): | |
arr[i].append(j) | |
# print before | |
print "Before:" | |
for x in arr: | |
print x | |
offset = -1 | |
# rotate CW 90 degrees | |
for row in range(0, n): | |
offset += 1 | |
for col in range(offset, n): | |
temp = arr[offset][col] | |
arr[offset][col] = arr[col][offset] | |
arr[col][offset] = temp | |
# print after | |
print "After:" | |
for x in arr: | |
print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment