Answer to Question #200345 in Python for sudheer

Question #200345

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


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.Output


The output should be M lines containing the ordered matrix.

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

can anyone please give the code?


1
Expert's answer
2021-06-01T03:06:03-0400
M, N = [int(x) for x in input('Input M and N: ').split()]
nums = []
for i in range(M):
    nums.append([int(x) for x in input().split()])
values = [num for row in nums for num in row]
values.sort()

for i in range(M):
    for j in range(N):
        print(values[i * M + j], end=' ')
    print()

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