J - 20 char
Couldn't help it ;)
(}.@}:#~0>2*/\2-/\])
Explanation follows:
2-/\]
- Over each pair of elements in the argument (each 2-item long infix), take the difference.2*/\
- Now over each pair of the new list, take the product.0>
- Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.}.@}:
- Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.#~
- Use the true values on the right side to pick items from the list on the left side.
Usage:
(}.@}:#~0>2*/\2-/\]) 1 2 1
2
(}.@}:#~0>2*/\2-/\]) 0 1 0 1 0
1 0 1
(}.@}:#~0>2*/\2-/\]) i.0 NB. i.0 is the empty list (empty result also)
(}.@}:#~0>2*/\2-/\]) 3 4 4 4 2 5
2