|
14 | 14 | [3,9,3]] |
15 | 15 |
|
16 | 16 | """ |
17 | | -def transpose_matrix(matrix: list[list[int]]) ->list[list[int]]: |
18 | | - """"" |
| 17 | + |
| 18 | + |
| 19 | +def transpose_matrix(matrix: list[list[int]]) -> list[list[int]]: |
| 20 | + """ "" |
19 | 21 | creating a new empty matrix for storing transposed values |
20 | 22 | number of rows in the matrix=len(matrix) |
21 | 23 | number of columns =number of elements in the matrix=number of element in 1st row of the matrix=len(matrix[0]) |
22 | 24 | """ |
23 | | - |
| 25 | + |
24 | 26 | # creating a new empty matrix for storing transposed values |
25 | 27 | # number of rows in the matrix=len(matrix) |
26 | 28 | # number of columns =number of elements in the matrix=number of element in 1st row of the matrix |
27 | 29 | # =len(matrix[0]) |
28 | | - |
29 | | - transposed_matrix=[[0]*len(matrix) for _ in range(len(matrix[0]))] |
30 | | - """ |
| 30 | + |
| 31 | + transposed_matrix = [[0] * len(matrix) for _ in range(len(matrix[0]))] |
| 32 | + """ |
31 | 33 | created an empty matrix of dimension len(matrix)*len(matrix[0]) |
32 | 34 | """ |
33 | 35 | for i in range(len(matrix)): |
34 | 36 | for j in range(len(matrix[0])): |
35 | | - """ |
| 37 | + """ |
36 | 38 | traversing the matrix element-by-element starting from 1st element of 1st row to last element of last row |
37 | 39 | 1st loop--> traversing through the row |
38 | 40 | 2nd loop--> traversing through the column |
39 | 41 | by this whole matrix is traversing |
40 | | - |
41 | | - # traversing the matrix element-by-element starting from |
| 42 | +
|
| 43 | + # traversing the matrix element-by-element starting from |
42 | 44 | # 1st element of 1st row to last element of last row |
43 | 45 | # 1st loop--> traversing through the row |
44 | 46 | # 2nd loop--> traversing through the column |
45 | 47 | # by this whole matrix is traversing |
46 | 48 |
|
47 | | - """ |
48 | | - transposed_matrix[j][i]=matrix[i][j] |
| 49 | + """ |
| 50 | + transposed_matrix[j][i] = matrix[i][j] |
49 | 51 | """ |
50 | 52 | keeping the values of matrix to resultant matrix in transposed order |
51 | 53 | for example 2nd element of 3rd row will be 3rd element of 2nd row |
52 | 54 | 1nd element of 2rd row will be 2rd element of 1nd row |
53 | | - likwise diagonal element will reamin intact |
| 55 | + likwise diagonal element will reamin intact |
54 | 56 |
|
55 | | - """ |
56 | | - #return the transposed_matrix |
| 57 | + """ |
| 58 | + # return the transposed_matrix |
57 | 59 | return transposed_matrix |
| 60 | + |
| 61 | + |
58 | 62 | """ |
59 | 63 | check for main function |
60 | 64 | give input and call the transpose_matrix () function with matirx as a parameter |
61 | 65 | """ |
62 | | -if __name__=="__main__": |
63 | | - matrix=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] |
| 66 | +if __name__ == "__main__": |
| 67 | + matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] |
64 | 68 | print(transpose_matrix(matrix)) |
0 commit comments