Python Program to Print Matrix after ordering all elements in increasing order

 

Ordered Matrix in Increasing order

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.

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

Sample Input 1
3 3
1 21 4
40 20 3
6 11 15

Sample Output 1
1 3 4
6 11 15
20 21 40

Code:

m,n=input().split()
m=int(m)
n=int(n)
num_list=[]
for i in range(m):
    lines=input().split()
    for j in lines:
        j=int(j)
        num_list+=[j]
num_list.sort()
a=0
b=n
for rows in range(m):
    result=(num_list[a:b])
    for i in result:
        print(i,end=" ")
    print()
    a+=n
    b+=n
        
        
    

Input
3 4
1 21 4 9
40 20 3 12
6 11 15 18

Output
1 3 4 6
9 11 12 15
18 20 21 40

Comments

Popular posts from this blog

CODING ASSIGNMENT 4

CODING PRACTICE 11 Music Page

CCBP Static Website Coding Assignment 2 Solution | Yoga Page