Answer to Question #201533 in Python for sudheer

Question #201533

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?


1
Expert's answer
2021-06-01T03:03:27-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS