Skip to content

Commit f20bf90

Browse files
committed
simplify implementation and fix bug and do stuff less times
1 parent 6f727d8 commit f20bf90

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

domain_model.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,30 @@ def skus(d):
88
return d.keys()
99

1010

11-
def allocate_line(sku, quantity, source, allocations):
12-
if source.get(sku, 0) > quantity:
13-
allocations[sku] = source
14-
15-
1611
def allocate_to(order, source):
1712
allocations = {}
1813
for sku, quantity in order.items():
19-
allocate_line(sku, quantity, source, allocations)
14+
if source.get(sku, 0) > quantity:
15+
allocations[sku] = source
2016
return allocations
2117

2218

23-
def allocate_to_sources(order, sources):
24-
allocations = {}
25-
for sku, quantity in order.items():
26-
for source in sources:
27-
allocate_line(sku, quantity, source, allocations)
28-
if sku in allocations:
29-
break
30-
return allocations
31-
32-
33-
3419
def allocate(order, stock, shipments):
35-
if skus(order) <= skus(stock):
36-
return allocate_to(order, stock)
20+
stock_allocation = allocate_to(order, stock)
21+
if set(stock_allocation) == set(order):
22+
return stock_allocation
3723

3824
shipments.sort(key=lambda s: s.eta)
3925

26+
shipment_allocations = []
4027
for shipment in shipments:
41-
if skus(order) <= skus(shipment):
42-
return allocate_to(order, shipment)
28+
shipment_allocation = allocate_to(order, shipment)
29+
if set(shipment_allocation) == set(order):
30+
return shipment_allocation
31+
shipment_allocations.append(shipment_allocation)
32+
33+
mixed_allocation = {}
34+
for allocation in shipment_allocations + [stock_allocation]:
35+
mixed_allocation.update(allocation)
36+
return mixed_allocation
4337

44-
return allocate_to_sources(order, [stock] + shipments)

0 commit comments

Comments
 (0)