Fix the batch planner for album thumbnail resizing
Your task
Your Lambda resized every image in an uploaded album inside one invocation. Large albums ran past Lambda's 15 minute limit, the function was killed in the middle of the loop, and the album was left half done. The new design fans out: a planner splits the images that still need resizing into small batches, and each batch runs in its own invocation.
An assistant wrote plan_resize_batches(keys, done_keys, max_per_batch). It is in the editor. It has a bug. Find it and fix it.
Input and output:
keys: the object keys in the album, in listing order. Each key is a string. A key can repeat, and a key can be the empty string"".done_keys: the keys an earlier run already resized. It can hold keys that are not inkeys.max_per_batch: the most keys one invocation may handle. An integer. It can be 0 or negative.- Return a list of batches. Each batch is a list of keys.
Rules the tests check:
- A key is pending when it is not
"", is not indone_keys, and is not a repeat of an earlier key inkeys. Keys compare exactly. - Pending keys keep the order in which they first appear in
keys. - The batch size is
max_per_batch, or 1 whenmax_per_batchis less than 1. - Split the pending keys, in order, into batches of the batch size. Every batch is full except possibly the last one.
- With no pending keys, return an empty list.
Examples
plan_resize_batches(keys: list[str], done_keys: list[str], max_per_batch: int) -> list[list[str]]Example 1
Input
plan_resize_batches(["album/1.jpg", "album/2.jpg", "album/3.jpg", "album/4.jpg", "album/5.jpg"], [], 2)Output
[["album/1.jpg", "album/2.jpg"], ["album/3.jpg", "album/4.jpg"], ["album/5.jpg"]]Example 2
Input
plan_resize_batches(["album/1.jpg", "album/2.jpg", "album/3.jpg"], ["album/2.jpg"], 2)Output
[["album/1.jpg", "album/3.jpg"]]Example 3
Input
plan_resize_batches(["album/1.jpg", "album/2.jpg"], ["album/1.jpg", "album/2.jpg"], 5)Output
[]