Answer to Question #166748 in Python for Chandra sena reddy

Question #166748

Given a MxN matrix,write a program to print all Anti-Diagonals elements of matrix

Input

The first line of input will contain a M, N values separated by space.

The second line will contain matrix A of dimensions MxN.

Output

The output should contain anti-diagonal elements separated by a line.

Explanation

For example, if M = 4, N = 4

Matrix A:


4 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16


so the output should be


1

2 5

3 6 9

4 7 10 13

8 11 14

12 15

16


1
Expert's answer
2021-03-01T04:58:37-0500
A = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12],
     [13, 14, 15, 16]]


M = len(A)
N = len(A[0])
print("%d %d"%(M,N))
for col in range(N):
    scol = col
    srow = 0
    while(scol >= 0 and srow < N):
           print(A[srow][scol], end = " ")
           scol = scol-1
           srow = srow+1
    print("")
for row in range(1, N):
    srow = row
    scol = N - 1
    while(srow < N and scol >= 0):
          print(A[srow][scol],end=" ")
          scol = scol-1
          srow = srow+1
    print("")


Python Output:





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
APPROVED BY CLIENTS