PY-11

Split a Sequence into Fixed-Size Groups

  • Easy–Medium
  • Sequences and Grouping
  • Python

Task

Write split_groups(values, size, remainder_policy). Divide values into consecutive groups of at most size items, preserving order. size is a positive integer. The final group may be shorter when the input length is not a multiple of size.

remainder_policy is one of three strings:

  • "keep": return every group, including a shorter final group;
  • "drop": omit a shorter final group;
  • "report": return a two-item tuple (full_groups, remainder), where full_groups contains only complete groups and remainder is the shorter final list. When there is no remainder, remainder is an empty list.

For "keep" and "drop", return the groups list directly. For "report", return the tuple described above. An empty input has no groups and no remainder. Do not change values.

Example

When the length divides evenly, "report" still returns a tuple, with an empty remainder:

Your implementation

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

values is a list, size is positive, and remainder_policy is exactly one of "keep", "drop", or "report". Return the form required by that policy. The input list must remain unchanged; do not print or ask for input.