Matrix Diagonal Sum
mat is a square grid (as many rows as columns). Add up the numbers on the main diagonal (top-left to bottom-right) and on the other diagonal (top-right to bottom-left). If a cell lies on both, count it only once.
Example 1
mat [[1,2,3],[4,5,6],[7,8,9]]
returns 25
1+5+9 and 3+7, and the middle 5 is counted once: 25.
Example 2
mat [[1,2],[3,4]]
returns 10
1+4 and 2+3 = 10.
Constraints
1 <= len(mat) <= 8- The grid is square.
-50 <= mat[r][c] <= 50
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.