Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate n*n matrix inplace #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions rotatenbyn_inplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
Question Statement
Rotate a n*n matrix by 90 degrees without using extra space

'''
N = 4

def rotateMatrix(matrix):

for i in range(0, int(N/2)):

for j in range(i, N-i-1):

tmp = matrix[i][j]

matrix[i][j] = matrix[j][N-1-i]

matrix[j][N-1-i] = matrix[N-1-i][N-1-j]

matrix[N-1-i][N-1-j] = matrix[N-1-j][i]

matrix[N-1-j][i] = tmp


def displayMatrix( matrix ):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function doesn't display matrix in a matrix-format


for i in range(0, N):

for j in range(0, N):

print (matrix[i][j])
print ("")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please follow PEP8 style guide!




matrix = [[0 for i in range(N)] for j in range(N)]
matrix = [ [0, 1, 2, 3 ],
[00, 11, 22, 33 ],
[44, 55, 66, 77 ],
[88, 99, 110, 120 ] ]
rotateMatrix(matrix)

displayMatrix(matrix)