Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Clockwise matrix rotation
#python 3.6.9 ''' The only thing that is different is to print the elements of cycle in clockwise direction i.e. An N x N matrix will have floor(N/2) square cycles. For example, a 3 X 3 matrix will have 1 cycle. The cycle is formed by its 1st row, last column, last row, and 1st column. For each square cycle, we swap the elements involved with the corresponding cell in the matrix in the clockwise direction. We just need a temporary variable for this. ''' def rotate90Clockwise(A): N = len(A[0]) for i in range(N // 2): for j in range(i, N - i - 1): temp = A[i][j] A[i][j] = A[N - 1 - j][i] A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j] A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i] A[j][N - 1 - i] = temp # Function to print the matrix def printMatrix(A): N = len(A[0]) for i in range(N): print(A[i]) # Driver code A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] rotate90Clockwise(A) printMatrix(A)
run
|
edit
|
history
|
help
0
double
L4-HW4-Finall
Faiha - L6
XII-F
try1
hi1
BubbleSort
PyBoolNone
Lesson8
python_study_note_for loop@nested loop