PY-13

Take a Bounded Window around a Position

  • Easy–Medium
  • Sequences and Boundaries
  • Python

Task

Write bounded_window(values, position, radius). position is a valid index in values, and radius is a non-negative integer. Build a window containing the selected value, up to radius values before it, and up to radius values after it.

Return a five-item tuple:

(window, start, stop, truncated_left, truncated_right)
  • window is a new list containing values[start:stop].
  • start is the included window's zero-based start index.
  • stop is its half-open stop index: the value at stop is not included.
  • truncated_left is True when the requested left boundary would be before index 0, and False otherwise.
  • truncated_right is True when the requested right boundary would be after the list, and False otherwise.

Clip the boundaries to the list. The input list must not change.

Example

At an edge, the window is shorter and records which side was clipped:

Your implementation

Edit solution.py and keep this function name and signature:

Return the required tuple; do not print it or ask for input. You may assume that values is non-empty, position is valid, and radius is non-negative.