![]() |
VOOZH | about |
Let’s solve a common problem where we find the strongest neighbour in a list of numbers. By "strongest neighbour," we mean the maximum of every two adjacent numbers in the list. For instance, if we have [1, 2, 3, 4], the strongest neighbours would be [2, 3, 4]. This is because 2 is the maximum of 1 and 2, 3 of 2 and 3 and so on. Let's go through a few methods to tackle this problem.
We can pair adjacent elements using the zip() function and then find the maximum of each pair. This approach is clean and efficient.
[2, 3, 4, 5]
Let's explore some more methods and see how we can find the strongest neighbor.
This method is similar to the first but skips the use of zip() by manually calculating the indices of adjacent elements.
[2, 3, 4, 5]
This is the more traditional approach where we explicitly loop through the list to calculate the strongest neighbours. It’s straightforward but slightly longer.
[2, 3, 4, 5]
If we prefer a functional approach, we can use map() along with a lambda function to achieve the same result.
[2, 3, 4, 5]