Hilltops
Inputs
cells is a grid: a list of 1 to 5 rows, each with the same number (1 to 5) of whole numbers from 0 to 9.
Your tasks
- Create a number
peaksthat starts at 0. - Use nested loops over every cell. A cell is a peak when it is strictly larger than each neighbour directly above, below, left and right. Cells next to the edge just have fewer neighbours.
- Add 1 to
peaksfor each peak. - Complete
countPeaksso it returnspeaks. Return it, do not print it.
Example
With cells = [[1, 2, 1], [2, 5, 2], [1, 2, 1]], countPeaks returns 1.
Rules
- Diagonal neighbours are ignored.
- A cell equal to a neighbour is not a peak.
Test data
TEST DATA 1: cells is [[1, 2, 1], [2, 5, 2], [1, 2, 1]]
TEST DATA 2: cells is [[7]]
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.