Print the Diagonals of a Matrix
Problem to find the Diagonals of a Matrix.
If matrix is like this, 1 2 3 4 4 3 2 1 7 8 9 6 6 5 4 3 The Diagonals are 1,3,9,3.
Example-1:
Input : n = [[ 1, 2, 3, 4],[ 4, 3, 2, 1],[ 7, 8, 9, 6],[ 6, 5, 4, 3]] Output : [1, 3, 9, 3]
Example-2:
Input : n = [[ 1, 2, 3],[ 4, 5, 6],[ 7, 8, 9]] Output : [1, 5, 9]
Solution
n = [[ 1, 2, 3, 4],[ 4, 3, 2, 1],[ 7, 8, 9, 6],[ 6, 5, 4, 3]] l = [] for i in range(len(n)): l.append(n[i][i]) print(l)
Output
[1, 3, 9, 3]