Skip to content

Instantly share code, notes, and snippets.

@yokiy
Created June 20, 2014 22:16
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 yokiy/082210d39d5254f540f6 to your computer and use it in GitHub Desktop.
Save yokiy/082210d39d5254f540f6 to your computer and use it in GitHub Desktop.
cc 1.6
#cc 1.6 Given an image represented by an N*N matrix, where each pixel in the image is 4 bytes. write a method to rotate the image by 90 degrees
#Assume clockwise rotation
def rotate(matrix):
n = len(matrix)
for layer in range(0, n/2):
first = layer
last = n-layer-1
for i in range(first, last):
offset = i - first
'''save the top'''
top = matrix[first][i]
'''left to top'''
matrix[first][i] = matrix[last - offset][first]
'''bottom to left'''
matrix[last - offset] [first] = matrix[last][last - offset]
'''right to bottom'''
matrix[last][last - offset] = matrix[i][last]
'''top to right'''
matrix[i][last] = top
return matrix
#test case
matrix = ([1,2,3,6,7], [4,5,6,9,10], [7,8,9,5,6], [5,7,8,3,2], [8,6,5,0,7])
print 'the original matrix:', matrix
print 'After rotation:', rotate(matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment