Answer to Question #166408 in Python for Chandra sena reddy

Question #166408

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:


1
Expert's answer
2021-02-24T20:40:23-0500
m, n = input('Enter the values of M and N separated by a space ').split()
m = int(m)
n  = int(n)
matrix_str = input('Enter all matrix values separated by a space ').split()
# create matrix
matrix = [matrix_str[n*i:n*(i+1)] for i in range(m)]
# print anti diagonal elemen in first line
for i in range(n):
    for j in range(i+1):
        try:
            print(matrix[j][i-j],end=' ')
        except:
            break
    print('')
# print anti-diagonal elements of the last column starting from line 2
for i in range(1,m):
    for j in range(i+1):
        try:
            print(matrix[i+j][-1-j],end=' ')
        except:
            break
    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