Answer to Question #206040 in Python for Ayushi

Question #206040


Ordered Matrix:



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:

3 3

1 20 3

30 10 2

5 11 15



output:

[[1, 2, 3], [5, 10, 11], [15, 20, 30]]


expected output:

1 2 3 
5 10 11 
15 20 30 


input:

2 5

-50 20 3 25 -20

88 17 38 72 -10


expected output:


-50 -20 -10 3 17 
20 25 38 72 88 


please rectify it:



1
Expert's answer
2021-06-11T14:12:20-0400
M=int(input(""))
N=int(input(""))
matrix= []
for i in range(0,M):
    valstr=input("")
    vals=valstr.split()
    for v in vals:
        matrix.append(int(v))


matrix.sort()
matrix2=[]
matrix2 = [matrix[i*N:(i+1)*N] for i in range(0,M)]
print()
for r in matrix2:
    for el in r:
        print(el, 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