Index arithmetic & neighbours
Many questions compare an item with the one before or after it. The neighbours of position i are i - 1 and i + 1:
left = temps[i - 1] right = temps[i + 1]
Two traps live here:
- The ends have no neighbour.
temps[-1]does not raise an error ati = 0: it quietly reads the last item. Andtemps[len(temps)]raisesIndexError. Start a loop at 1, or stop it atlen(temps) - 1, when you need both sides. - Short inputs. A list with 0, 1 or 2 items may have no position with two neighbours.
range(1, len(x) - 1)is simply empty then, which is usually what you want.
To wrap around a ring, use the remainder: (i + 1) % n is 0 when i is the last position.
Watch the highlight: temps[i] moves with i, and the neighbours sit one cell either side.
See it run
temps = [3, 7, 5, 9] i = 1 left = temps[i - 1] right = temps[i + 1] i = 3 wrapped = temps[(i + 1) % 4]
A list in indexed cells. The highlight follows temps[i].
Practice index arithmetic & neighbours
Exercise 1
A trail has the elevations in heights, one per marker. A marker is a summit when it is strictly higher than the marker before it and strictly higher than the marker after it. The first and last markers have only one neighbour, so they are never summits.
Return how many summits the trail has, as an integer.
Rules:
- Equal neighbours do not count: both must be strictly lower.
- Trails with fewer than 3 markers have no summits.
Constraints: 0 ≤ len(heights) ≤ 1000, each height between -1000 and 1000.
Example: solution([1, 3, 2, 4, 1]) returns 2 because 3 and 4 each beat both neighbours.
Exercise 2
Lamps hang on a circular string, and colours lists them in order around the circle. The last lamp is next to the first. A lamp is a match when the next lamp around the circle has the same colour.
Return how many lamps are matches, as an integer.
Rules:
- Every lamp is compared with the next one, and the last lamp is compared with the first.
- With fewer than 2 lamps the answer is
0. With exactly 2 lamps of the same colour, both are matches (each is next to the other).
Constraints: 0 ≤ len(colours) ≤ 1000, colours are strings.
Example: solution(["r", "g", "g", "r"]) returns 2: the first g matches the second g, and the last r matches the first r.
Exercise 3
A sensor logs whole-number readings in readings. Smooth the log: each position becomes the average of itself and its neighbours that exist, rounded down (floor, so -3.5 becomes -4).
The first position averages itself with the second, the last averages itself with the one before, and every other position averages three readings. A single reading stays as it is.
Return the smoothed readings as a new list of whole numbers, same length. Use the original readings for every average, not already-smoothed ones.
Constraints: 0 ≤ len(readings) ≤ 1000, each reading between -10^6 and 10^6.
Example: solution([3, 9, 6]) returns [6, 6, 7] because (3+9)//2 = 6, (3+9+6)//3 = 6 and (9+6)//2 = 7.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.