Generate distribution plans

Write codeEasy17 min

Your task

A distributor has total_cases identical cases to send out this week and must place every one of them. store_names lists the stores in delivery order, and store_caps[i] is the most cases store i has room for, which may be zero. A plan gives every store a whole number of cases, from zero up to its own cap, and the shares add up to total_cases exactly. Return every plan as a string: each store name, an equals sign, then its share, joined by a comma and a space, so two stores read as "north=1, south=2". Build the plans by giving the first store zero cases first, then one, and so on up to what it can take, recursing on the cases that are left, so the plans come back in ascending order of the first store's share. Return an empty list when no plan fits, and when there are no stores. There are at most 6 stores.

Examples

list_store_allocations(store_names: list[str], store_caps: list[int], total_cases: int) -> list[str]
Example 1
Inputlist_store_allocations(["north", "south"], [2, 2], 3)
Output["north=1, south=2", "north=2, south=1"]
Example 2
Inputlist_store_allocations(["north", "south"], [2, 2], 0)
Output["north=0, south=0"]
Example 3
Inputlist_store_allocations(["north", "south"], [1, 1], 5)
Output[]