sorted_last_index_of()
This method is like `sorted_last_index` except that it returns the index of the closest element to the supplied `value` in a sorted `array`. If `value` is not found, the index of the next closest element lower in value is returned.
Implementation
Args: array: Array to inspect. value: Value to evaluate. Returns: The index of the closest value to `value`. If `value` is not found, the index of the next closest element lower in value.
Example
sorted_last_index_of([1, 2, 3, 4], 4.2)
Expected output: 3
Source Code
def sorted_last_index_of(array: List[T], value: T) -> int:
idx = array[::-1].index(value) if value in array else -1
return len(array) - idx - 1 if idx >= 0 else -1