← Back to challenges

Group Monotonicity

PythonHardalgorithmsmath

Instructions

Create a function which returns the indices where the monotonicity of a 1-D list changes. If there are none, return an empty list. A monotonic list is one that is either non-increasing or non-decreasing.

Examples

group_monotonic([0, 1]) ➞ []

group_monotonic([0, 2, 1]) ➞ [1]

group_monotonic([0, 1, 1, 0]) ➞ [2]

Notes

  • Trivially, all points and line-segments are monotonic (see example #1).
  • Return the indices where each monotonic section stops, not where each new one begins: i.e. return the "peaks" of the triangles (see example #2).
  • Monotonic lists are allowed to be constant (see example #3).
  • You can expect positive and negative values in the list.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.