Skip to content

Instantly share code, notes, and snippets.

@thenonameguy
Created December 8, 2012 14:14
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 thenonameguy/4240404 to your computer and use it in GitHub Desktop.
Save thenonameguy/4240404 to your computer and use it in GitHub Desktop.
NxN Matrix rotation
class SquareMatrix():
"""Square matrix implementation by thenonameguy"""
def __init__(self,size):
self.size = size
self.emptyMatrix()
def emptyMatrix(self):
self.matrix=[[0 for i in range(self.size)]for j in range(self.size)]
def makeIdentity(self):
self.emptyMatrix()
for i in range(0,self.size):
self.matrix[i][i]=1
def print(self):
for i in range(0,self.size):
print("[",end='')
for j in range(0,self.size):
print(self.matrix[i][j],end='')
if j<self.size-1:
print(",",end='');
print("]")
def rotate(self):
for i in range(0,self.size):
for j in range(0,self.size):
foo=self.matrix[j][self.size-i-1]
self.matrix[j][self.size-i-1]=self.matrix[i][j]
self.matrix[i][j]=foo
if __name__=="__main__":
x=SquareMatrix(4)
x.makeIdentity()
x.matrix[2][0]=8
x.print()
print("Rotating...")
x.rotate()
x.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment