Answer to Question #175896 in Python for jayanth

Question #175896

Given a M x N matrix, write a program to print the matrix after ordering all the elements of the matrix in increasing order.


The first line of input will contain two space-separated integers, denoting the M and N.

The next M following lines will contain N space-separated integers, denoting the elements of each list.


The output should be M lines containing the ordered matrix.

Note: There is a space at the end of each line.Explanation


For example, if the given M is 3 and N is 3, read the inputs in the next three lines if the numbers given in the next three lines are the following.

1 20 3
30 10 2
5 11 15


By ordering all the elements of the matrix in increasing order, the ordered matrix should be

1 2 3
5 10 11
15 20 30
1
Expert's answer
2021-03-26T13:07:37-0400
M, N = input().split(' ')
M, N = int(M), int(N)
matrix = []
for _ in range(M):
	row = [int(x) for x in input().split(' ')]
	matrix.append(row)


values = [x for row in matrix for x 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)

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

siri
03.05.21, 11:01

for that answer [[1, 2, 3], [5, 10, 11], [15, 20, 30]] this is ouput .but the expected output is same number houls not be in matrix,should be in line by line.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS