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:
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()
Comments
Leave a comment