cumsub
cumsub, function
def process cumsub(
item: text,
stock: number,
quantity: number,
rank: number) : number
Computes remaining stock per item by processing bundles ordered by increasing rank. If the current stock covers the bundle’s total quantity, it decrements the stock; otherwise it outputs -(S+1) where S is the current stock.
item: the item identifier; all lines with the sameitembelong together.stock: the initial stock for the item (identical across lines with the sameitem).quantity: the required quantity for each line.rank: the bundle identifier; each(item, rank)pair must be unique.
Examples
table G = with
[| as Item, as Stock, as Quantity, as Rank |]
[| "A", 10, 4, 1 |]
[| "B", 5, 2, 1 |]
[| "A", 10, 3, 2 |]
[| "B", 5, 3, 2 |]
[| "A", 10, 2, 3 |]
[| "B", 5, 1, 4 |]
G.Remaining = cumsub(G.Item, G.Stock, G.Quantity, G.Rank)
show table "Result inventory" with
G.Item
G.Stock
G.Quantity
G.Rank
G.Remaining
Output:
| Item | Stock | Quantity | Rank | Remaining |
|---|---|---|---|---|
| A | 10 | 4 | 1 | 6 |
| B | 5 | 2 | 1 | 3 |
| A | 10 | 3 | 2 | 3 |
| B | 5 | 3 | 2 | 0 |
| A | 10 | 2 | 3 | 1 |
| B | 5 | 1 | 4 | -1 |