Drill

ProblemsPython › orders

Split an order across warehouses

mediumordersPython

Fulfilment needs the order regrouped by the warehouse each line ships from, so it can raise one pick list per site.

split_by_warehouse(rows: list<Row>) → map<string, list<string>>

Solve it in the editor →

Where you start

def split_by_warehouse(rows: list[Row]) -> dict[str, list[str]]:
    

Worked examples

CallResult
split_by_warehouse([Row(sku="A1", warehouse="IST"), Row(sku="B2", warehouse="ANK"), Row(sku="C3", warehouse="IST")]){"IST": ["A1", "C3"], "ANK": ["B2"]}
split_by_warehouse([Row(sku="A1", warehouse="IST")]){"IST": ["A1"]}
split_by_warehouse([]){}
split_by_warehouse([Row(sku="X", warehouse="IZM"), Row(sku="X", warehouse="IZM")]){"IZM": ["X", "X"]}

Hint

A map from code to a growing list. Create the list the first time you meet a code.

Reference solution in Python
def split_by_warehouse(rows: list[Row]) -> dict[str, list[str]]:
    result = {}
    for r in rows:
        result.setdefault(r.warehouse, []).append(r.sku)
    return result

The same problem in another language

More orders problems in Python