How to print M by N anti diagonal matrix ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print M by N anti diagonal matrix ?

Input: 2 3 1 5 5 2 7 8 Output : 1 5 2 5 7 8

16th Oct 2022, 5:31 PM
PRAVEEN
PRAVEEN - avatar
1 Answer
0
Try something like this: def print_anti_diagonal_matrix(matrix): rows = len(matrix) cols = len(matrix[0]) for k in range(rows + cols - 1): row_start = max(0, k - cols + 1) row_end = min(rows, k + 1) for i in range(row_end - 1, row_start - 1, -1): j = k - i if i >= 0 and i < rows and j >= 0 and j < cols: print(matrix[i][j], end=" ") print() M, N = map(int, input().split()) matrix = [] for _ in range(M): row = list(map(int, input().split())) matrix.append(row) print_anti_diagonal_matrix(matrix)
2nd Jun 2023, 5:30 PM
AgentSmith