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
1 Odpowiedź
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)