Shipping rates
MediumOnline Assessment
Price the parcels of a small online shop from its rate table, where one rule can be overridden by a more specific one.
Each rule is a string "region|category|flat|perkg". The region and the category are either an exact name or *, which matches anything. flat is the price in cents of the first kilogram, perkg is the price in cents of every further kilogram. A flat of X means the shop does not ship this combination (then perkg is ignored).
Each order is a string "region|category|grams". A rule applies to an order if its region matches the order's region and its category matches the order's category (names are case-sensitive). When several rules apply, the winner is chosen like this:
- The rule with the most specific match wins, from most to least specific: exact region and exact category; exact region with
*category;*region with exact category;*and*. - If two applicable rules are equally specific, the one that appears later in
ruleswins.
The weight is billed in whole kilograms, rounded up, and at least 1 kg: kg = max(1, ceil(grams / 1000)). The price of an order is flat + perkg * (kg - 1).
Input: rules, a list of rule strings, and orders, a list of order strings.
Output: a list of integers, one per order in the same order: the price in cents, or -1 if the winning rule is a X rule or no rule applies. If orders is empty, return [].
Example 1
- Input:
- [["*|*|500|100","EU|*|400|80","*|books|300|50","EU|books|250|40"],["US|toys|1500","EU|toys|1000","US|books|3000","EU|books|1001"]]
- Output:
- [600,400,400,290]
- Explanation
- Every order matches several rules. "EU|toys|1000" is exactly 1 kg and matches "EU|*" (more specific than "*|*"), giving 400. "US|toys|1500" rounds up to 2 kg and only matches "*|*": 500 + 100 = 600. "US|books|3000": the books rule beats "*|*", 300 + 50*2 = 400. "EU|books|1001" matches the exact rule: 250 + 40 = 290.
Example 2
- Input:
- [["*|*|500|100","EU|*|400|80","*|books|300|50"],["EU|books|2500","US|books|1","AS|toys|999"]]
- Output:
- [560,300,500]
- Explanation
- "EU|books|2500" matches both "EU|*" and "*|books". An exact region beats an exact category, so "EU|*" wins: 3 kg, 400 + 80*2 = 560. "US|books|1" is billed as 1 kg by the books rule: 300. "AS|toys|999" falls back to the catch-all: 500.
Example 3
- Input:
- [["EU|*|400|80","EU|*|450|60","*|batteries|X|0","EU|batteries|200|10","AS|*|X|0","AS|*|700|0"],["EU|toys|3000","US|batteries|500","EU|batteries|1000","AS|toys|100","US|toys|100"]]
- Output:
- [570,-1,200,700,-1]
- Explanation
- For EU toys the two "EU|*" rules are equally specific and the later one wins: 450 + 60*2 = 570. "US|batteries" hits an X rule, so -1. "EU|batteries|1000" is matched exactly (rule "EU|batteries", 200) and is not blocked by "*|batteries". For AS the later "AS|*" rule replaces the earlier X rule: 700. "US|toys" matches no rule: -1.
Constraints
0 <= rules.length <= 100,0 <= orders.length <= 3001 <= grams <= 500000;flatandperkgare integers between 0 and 100000 (flatmay beXinstead)- Region and category names are non-empty, contain no
|and are never the single character*except as the wildcard