-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspose_matrix.py
More file actions
38 lines (29 loc) · 962 Bytes
/
transpose_matrix.py
File metadata and controls
38 lines (29 loc) · 962 Bytes
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
28
29
30
31
32
33
34
35
36
37
38
import io
f = io.StringIO('''2 3
1 2 3
4 5 6''')
def transpose_matrix(matrix, rows, cols):
new_matrix = []
for col in range(cols):
new_matrix.append([0 for _ in range(rows)])
for row in range(cols):
for col in range(rows):
new_matrix[row][col] = matrix[col][row]
return new_matrix
def main(str_buffer):
rows, cols = [int(i) for i in next(str_buffer).split()]
matrix = create_matrix(cols, rows, str_buffer)
new_matrix = transpose_matrix(matrix, rows, cols)
for row in new_matrix:
print(' '.join(map(str, row)))
return new_matrix
def create_matrix(cols, rows, str_buffer):
matrix = []
for rows in range(rows):
matrix.append([0 for _ in range(cols)])
for i in range(rows + 1):
row_values = [int(i) for i in next(str_buffer).split()]
matrix[i] = row_values
return matrix
if __name__ == '__main__':
assert main(f) == [[1, 4], [2, 5], [3, 6]]