Drill

ProblemsGo › orders

Split an order across warehouses

mediumordersHash mapsArraysGo

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

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

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func splitByWarehouse(rows []Row) map[string][]string {
	
}

Worked examples

CallResult
splitByWarehouse([]Row{Row{Sku: "A1", Warehouse: "IST"}, Row{Sku: "B2", Warehouse: "ANK"}, Row{Sku: "C3", Warehouse: "IST"}})map[string][]string{"IST": []string{"A1", "C3"}, "ANK": []string{"B2"}}
splitByWarehouse([]Row{Row{Sku: "A1", Warehouse: "IST"}})map[string][]string{"IST": []string{"A1"}}
splitByWarehouse([]Row{})map[string][]string{}
splitByWarehouse([]Row{Row{Sku: "X", Warehouse: "IZM"}, Row{Sku: "X", Warehouse: "IZM"}})map[string][]string{"IZM": []string{"X", "X"}}

Hint

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

Reference solution in Go
func splitByWarehouse(rows []Row) map[string][]string {
	result := map[string][]string{}
	for _, r := range rows {
		result[r.Warehouse] = append(result[r.Warehouse], r.Sku)
	}
	return result
}

The same problem in another language

More orders problems in Go