def _window_has_intervening_extrema(window, contour, mode):
	"""
	Steps 8/9. If there exists a sequence of equal maxima or minima, check if the sequence
	contains an intervening opposite extrema, i.e. if a sequence of two equal maxima contains
	a minima between them.

	>>> maxima_group = [
	... 	(2, [2, {1}]),
	... 	(4, [2, {1}])
	... ]
	>>> contour = [[1, {1, -1}], [0, {-1}], [2, {1}], [0, {-1}], [2, {1}], [1, {1, -1}]]
	>>> _window_has_intervening_extrema(maxima_group, contour=contour, mode="max")
	True
	"""
	contour_index_range = [window[0][0], window[-1][0]]

	# Two trivial cases
	if len([x for x in window if 1 in x[1][1]]) == 1:  # Single extrema.
		return True
	elif window[0][0] + 1 == window[-1][0]:  # Two contiguous extrema.
		if mode == "max":
			return -1 in contour[contour_index_range[1]][1]
		if mode == "min":
			return 1 in contour[contour_index_range[1]][1]

	"""
	IMPORTANT NOTE: The conditions for intervening extrema are unclear from Schultz's paper.
	For example, are beginning/ending contour elements allowed to contain the opposite extrema
	value? I assume not, so I restrict the range.

	Include start: intervening_range = contour[window[0][0]:window[-1][0]+1]
	"""
	intervening_range = contour[contour_index_range[0] + 1:contour_index_range[-1] - 1 + 1]
	if mode == "max":  # Looking for min.
		return any(-1 in x[1] for x in intervening_range)
	if mode == "min":  # Looking for max.
		return any(1 in x[1] for x in intervening_range)