For this program, I want an exact output with all test cases passed and it should not have any brackets.
def rotate1(matrix, n):
a = len(matrix)
n = int(a)
b = int(n/2)
for row in range(0, b):
for col in range(row, (n- row - 1)):
temp = matrix[row][col]
matrix[row][col] = matrix[n - 1 - col][row]
matrix[n - 1 - col][row] = matrix[n - 1 - row][n- 1 - col]
matrix[n - 1 - row][n - 1 - col] = matrix[col][n - 1 - row]
matrix[col][n - 1 - row] = temp
return matrix
matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]]
n = len(matrix)
array = rotate1(matrix, n)
for col in array:
print(col, sep=" ")
Sample Input 1
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
Sample Output 1
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
Sample Input 2
3 4
1 2 3 4
10 11 12 5
9 8 7 6
2
Sample Output 2
9 10 1 2
8 11 12 3
7 6 5 4
def rotate1(matrix, n):
a = len(matrix)
n = int(a)
b = int(n/2)
for row in range(0, b):
for col in range(row, (n- row - 1)):
temp = matrix[row][col]
matrix[row][col] = matrix[n - 1 - col][row]
matrix[n - 1 - col][row] = matrix[n - 1 - row][n- 1 - col]
matrix[n - 1 - row][n - 1 - col] = matrix[col][n - 1 - row]
matrix[col][n - 1 - row] = temp
return matrix
matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]]
n = len(matrix)
array = rotate1(matrix, n)
for i in range(4):
for j in range(4):
print(array[i][j],end=" ")
print()
Comments
Leave a comment