sir above code correct #176332 i.e out put should be show
[13, 9, 5, 1, 2, 3, 4, 8, 12, 16, 15, 14]
[[13, 9, 5, 1], [14, 6, 7, 2], [15, 10, 11, 3], [16, 12, 8, 4]]
but original output should be show this manner
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
how to archive this
M, N = input().split(' ')
M, N = int(M), int(N)
matrix = []
for i in range(M):
row = [int(j) for j in input().split(' ')]
matrix.append(row)
values = [j for row in matrix for j in row]
values.sort()
output = matrix
for i in range(M):
for j in range(N):
output[i][j] = values[i * M + j]
print(output)
Comments
Leave a comment