The parity constraint is doing a lot of work here and I almost missed it.
First, clarify the problem constraints and edge cases, then derive the crop boundaries using integer division to compute the margins. Implement the solution by slicing the original grid row-wise and column-wise, and finally test with examples to verify correctness.
Pro tip: Mention that you would validate the parity assumption and handle potential edge cases like crop dimensions equal to image dimensions, demonstrating thoroughness and defensive coding.
Restate the problem to ensure clarity: given a 2D grayscale image (list of lists) and crop dimensions, return the centered subgrid. Note the parity assumption that ensures even margins.
Calculate the top and left margins as (image_height - crop_height) // 2 and (image_width - crop_width) // 2, respectively. The bottom and right boundaries are top + crop_height and left + crop_width.
Use list slicing to extract the subgrid: for each row from top to bottom, take the slice from left to right. Return the resulting list of lists.
Verify with a small example, including edge cases like crop dimensions equal to image dimensions, and confirm the output matches expectations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.