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)
input:
3 3
1 20 3
30 10 2
5 11 15
output:[[1, 2, 3], [5, 10, 11], [15, 20, 30]]
the output should be like this:
1 2 3
5 10 11
15 20 30
so, can anyone please give the correct code?
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]
for row in output:
print(*row)
Comments
Leave a comment