Diagonal
Inputs
cells is a square grid: n rows of n whole numbers each (n from 1 to 5), from -9 to 9.
Your tasks
- Create a number
totalthat starts at 0. - Use a single loop for
ifrom 0 up ton - 1. Addcells[i][i](the main diagonal) andcells[i][n - 1 - i](the other diagonal) tototal. - A cell on both diagonals, the middle of an odd-sized grid, must only be counted once.
- Complete
diagonalTotalso it returnstotal. Return it, do not print it.
Example
With cells = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], diagonalTotal returns 25.
Rules
- In a 1 by 1 grid, the single cell is counted once.
Test data
TEST DATA 1: cells is [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
TEST DATA 2: cells is [[5]]
Practice focus
Two-dimensional data is a loop over rows with a loop over columns inside. Grids lead straight to flood fill, islands, dynamic programming tables and path problems.