Build a system that calculates the price of a customer’s order in a furniture store. Pricing rules are as follows:
- Used items get a 20% discount off the original price.
- Buying multiple items of the same category (e.g., chairs) applies an additional discount:
- 2-3 items: 5% off
- 4-6 items: 10% off
- 7+ items: 15% off
- There is a category-based pricing tier for each item similar to the following:
CATEGORY_PRICES = {'chair': {'basic': 100, 'premium': 200, 'luxury': 300},
'table': {'basic': 250, 'premium': 500, 'luxury': 1000},
'sofa': {'basic': 500, 'premium': 1000, 'luxury': 2000},
'cabinet': {'basic': 300, 'premium': 600, 'luxury': 900}
}
Example usage:
items = [{'name': 'Classic Chair', 'category': 'chair', 'tier': 'basic', 'used': True},
{'name': 'Designer Chair', 'category': 'chair', 'tier': 'premium', 'used': False},
{'name': 'Luxury Table', 'category': 'table', 'tier': 'luxury', 'used': False}
]
Expected output:
- Basic Chair: 100 * 0.8 = 80 (used discount)
- Premium Chair: 200 (no used discount)
- Both chairs get 5% category discount
这道题考察的是订单计价规则的建模与批量折扣计算。做题时需要先根据商品的 category 和 tier 从价格表中取出基础价格,再对 used 商品先打 8 折;随后按同类商品数量应用阶梯式 category 折扣:2-3 件 95 折、4-6 件 9 折、7 件及以上 85 折。实现上通常会先按 category 分组,统计每组数量,再统一计算每个商品的最终价格,避免重复扣减或漏算折扣。
正文完