-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path032_problem.py
More file actions
27 lines (24 loc) · 1.02 KB
/
032_problem.py
File metadata and controls
27 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#rotate a matrix by 90 degrees
def rotate_matrix(matrix):
n = len(matrix)
# Create a new matrix with swapped dimensions
rotated = [[0] * n for _ in range(n)]
# Fill the rotated matrix
for i in range(n):
for j in range(n):
rotated[j][n - 1 - i] = matrix[i][j]
return rotated
# Get input for the matrix
rows = int(input("Enter the number of rows for the matrix: "))
cols = int(input("Enter the number of columns for the matrix: "))
matrix = []
print("Enter the elements of the matrix:")
for i in range(rows):
row = list(map(int, input().split()))
matrix.append(row)
# Rotate the matrix and print the result
rotated_matrix = rotate_matrix(matrix)
print("The rotated matrix is:")
for row in rotated_matrix:
print(*row)
#time complexity: O(n*m) where n is the number of rows and m is the number of columns in the matrix, because we need to iterate through each element of the matrix once. The space complexity is O(n*m) because we are creating a new matrix to store the rotated elements.