Skip to content

Instantly share code, notes, and snippets.

@diezguerra
Created October 17, 2014 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diezguerra/1b28eb8b7c28dfe4d6c6 to your computer and use it in GitHub Desktop.
Save diezguerra/1b28eb8b7c28dfe4d6c6 to your computer and use it in GitHub Desktop.
rot90 O(1) space
import numpy as np
arr = np.arange(1, 26).reshape((5, 5))
arr2 = np.arange(1, 26).reshape((5, 5))
def rot90(arr):
for circle in range(len(arr) // 2):
for item in range(0, len(arr) - 1 - 2 * circle):
sidey = len(arr) - 1 - circle
arr[circle][circle + item], \
arr[circle + item][sidey], \
arr[sidey][sidey - item], \
arr[sidey - item][circle] = \
arr[sidey - item][circle], \
arr[circle][circle + item], \
arr[circle + item][sidey], \
arr[sidey][sidey - item]
return arr
assert (np.rot90(arr, -1) == rot90(arr2)).all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment