Skip to content

Instantly share code, notes, and snippets.

@craigpalermo
Created September 4, 2014 15:15
Show Gist options
  • Save craigpalermo/ba10164f7426947a6240 to your computer and use it in GitHub Desktop.
Save craigpalermo/ba10164f7426947a6240 to your computer and use it in GitHub Desktop.
Interview question, rotate elements in 2D array 90 degrees clockwise
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