Answer to Question #255319 in Python for teja

Question #255319

Given an M x N matrix find all anti-diagonal elements of the matrix..

input :

the first input will contain values of M, N with separated by spaces..

the second line will contain matrix with M x N dimensions

i.e. input is

4 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16


output:

1

2 5

3 6 9

4 7 10 13

8 11 14

12 15

16



1
Expert's answer
2021-10-22T16:43:52-0400
M, N = map(int, input().split())
A = []

for _ in range(M):
    A.append(input().split())

for col in range(M):
    s_col = col
    s_row = 0
    while(s_col >= 0 and s_row < N):
        print(A[s_row][s_col], end=" ")
        s_col -= 1
        s_row += 1
    print()

for row in range(1, N):
    s_row = row
    s_col = N - 1
    while(s_row < N and s_col >= 0):
        print(A[s_row][s_col], end=" ")
        s_col -= 1
        s_row += 1
    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