Menu Close

Convert the square matrix to matrix in Z form

Given a square matrix of order n*n, we need to print elements of the matrix in Z form.
Square matrix means “A matrix having same or equal number of rows and columns”.

INPUT:
      1 0 1 0
      0 1 0 1
      1 0 1 0
      0 1 1 1


OUTPUT:
     1 0 1 0 
         0 
       0 
     0 1 1 1
def Zmatrx(Mx,r,c):
    for i in range(c-1):
       print(Mx[0][i],end=" ")
    for j in range(r-1):
        for k in range(c):
            if(j+k == c-1):
                print(Mx[j][k],end=" ")
                break
        print() #--> It is important
        print(end="  "*(k-1))
    for k in range(0,c):
        print(Mx[r-1][k],end=" ")
        

#Driver
'''
Matrx=[[4, 5, 6, 8],
       [1, 2, 3, 1],
       [7, 8, 9, 4],
       [1, 8, 7, 5]]

'''
#taking input manually
Matrx=[]
M=int(input("Row= "))
N=int(input("Column= "))

while True:
    arr=list(map(str,input().split(" ")))
    if arr != ['']:
        Matrx.append(list(map(int,arr)))
    else:
        break

Zmatrx(Matrx,M,N)

# WRITTEN BY legacyboy

INPUT_1:
Row= 4
Column= 4

1  0  1  0
0  1  0  1 
1  0  1  0
0  1  1  1

OUTPUT:
1  0  1  0
        0
    0
0  1  1  1


INPUT_2:
Row= 4
Column= 4

4  5  6  8
1  2  3  1 
7  8  9  4
1  8  7  5

OUTPUT:
4  5  6  8
        3 
    8
1  8  7  5


ILLUSTRATION:

EXECUTED USING PYTHON3

Morae Q!