Fix the property check for a chunking function
Your task
An AI agent wrote a Rust function that splits a vector into chunks of a fixed size. It compiles and the basic case looks right, but you would rather check its output than read its index arithmetic. So your team runs it on many inputs and checks each result with a property check: a function that says whether a result is a valid chunking.
An assistant wrote chunk_result_is_valid(items, size, result). It is in the editor. It has a bug. Find it and fix it.
Input and output:
items: the list that was split.size: the chunk size that was requested.result: the chunks the function returned, as a list of lists.- Return true when
resultis a valid chunking ofitems, and false otherwise.
Rules the tests check:
- If
sizeis 0 or less, the only valid result is an empty list. - Otherwise all of these must hold:
- The chunks joined in order give exactly
items: the same values, in the same order. - No chunk is empty. So an empty
itemsmust give an emptyresult. - Every chunk except the last has exactly
sizeitems. - The last chunk has from 1 to
sizeitems.
- The chunks joined in order give exactly
Examples
chunk_result_is_valid(items: list[int], size: int, result: list[list[int]]) -> boolExample 1
Input
chunk_result_is_valid([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4], [5]])Output
TrueExample 2
Input
chunk_result_is_valid([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4]])Output
FalseExample 3
Input
chunk_result_is_valid([1, 2, 3], 0, [])Output
True