1- def skus (d ):
2- return set (d .keys ())
3-
4- def allocated_completely (order , allocation ):
5- return skus (order ) == skus (allocation )
6-
1+ def allocate (order , stock , shipments ):
2+ allocations = []
3+ for source in [stock ] + shipments :
4+ allocation = allocate_to (order , source )
5+ if allocated_completely (order , allocation ):
6+ return allocation
7+ allocations .append (allocation )
8+ return combine_preferring_first (allocations )
79
810def allocate_to (order , source ):
911 return {
@@ -13,23 +15,12 @@ def allocate_to(order, source):
1315 and source [sku ] > quantity
1416 }
1517
18+ def allocated_completely (order , allocation ):
19+ return order .keys () == allocation .keys ()
1620
17- def merge (allocations ):
21+ def combine_preferring_first (allocations ):
1822 return {
1923 k : v
20- for d in allocations
24+ for d in reversed ( allocations )
2125 for k , v in d .items ()
2226 }
23-
24-
25-
26- def allocate (order , stock , shipments ):
27- allocations = []
28- for source in [stock ] + shipments :
29- allocation = allocate_to (order , source )
30- if allocated_completely (order , allocation ):
31- return allocation
32- allocations .append (allocation )
33-
34- return merge (reversed (allocations ))
35-
0 commit comments