Fix the property check for a chunking function

Review the AI's codeEasy10 min

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 result is a valid chunking of items, and false otherwise.

Rules the tests check:

  • If size is 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 items must give an empty result.
    • Every chunk except the last has exactly size items.
    • The last chunk has from 1 to size items.

Examples

chunk_result_is_valid(items: list[int], size: int, result: list[list[int]]) -> bool
Example 1
Inputchunk_result_is_valid([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4], [5]])
OutputTrue
Example 2
Inputchunk_result_is_valid([1, 2, 3, 4, 5], 2, [[1, 2], [3, 4]])
OutputFalse
Example 3
Inputchunk_result_is_valid([1, 2, 3], 0, [])
OutputTrue