注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
收到oa, 一个半小时大概做了一下,没什么特别难的可以注意下rounding。所有东西都整理好了贴出来了
新人求米新人求米
Part 1 — Initial
Problem
You're building a discount system for Trader Yojoe's, a growing grocery chain. Their business is thriving, but they're starting a new pilot program where shoppers can bring coupons and apply them to items at checkout. Their current point-of-sale system doesn't support discounts, so you need to build this functionality from scratch.
Given a list of items and a list of discount coupons, create an algorithm that correctly applies the discounts during purchase and calculates the final total.
Requirements
1. Each item has a name and price. The price is in integer cents.
2. Each discount coupon has an item name it applies to and a percent off.
3. Apply matching discounts to items and return a dictionary with three keys:
- subtotal: the sum of all original item prices
- total_discount: the sum of all discount amounts
- final_total: the subtotal minus the total discount
Assumptions
1. There will be at most one discount coupon per item name.
2. Discounts that don't match any item in the cart are ignored.
Notes
1. Round the final discount amount for each item to the nearest cent using round(). Do not round intermediate calculations.
2. Ensure all returned values are integers. Apply the Python 您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 使用VIP即刻解锁阅读权限或查看其他获取积分的方式 游客,您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 VIP即刻解锁阅读权限 或 查看其他获取积分的方式 Unlock interview details and practice with AI Curated Interview Questions from Top Companies
r item name.
2. There will be at most one category discount available per category.
3. You may activate any combination of available discounts, subject to the point budget of 20.
Notes
1. This is an optimization problem — focus on getting a working solution first, then improve it.
2. The auto-grader will first validate your output format, then score based on total savings achieved.
3. You will have unlimited submissions for Part 3.
4. Feel free to copy/paste your code from Part 2 or start from scratch.
Example
- items = [
- {"name": "Milk", "price": 500, "category": "Dairy"}
- ]
- discounts = [
- {"type": "item", "name": "Milk", "percent_off": 20}
- ]
复制代码
optimize_discounts(items, discounts) returns:
- {
- "selected_discounts": [
- {
- "type": "item",
- "name": "Milk",
- "percent_off": 20
- }
- ],
- "total_points_used": 2
- }
复制代码
apply_discounts_and_calculate_total(items, selected_discounts) returns:
- {
- "subtotal": 500,
- "total_discount": 100,
- "final_total": 400
- }
复制代码
Starter Code
- #!/bin/python3
- [size=6][b]Sample data[/b][/size]
- [size=6][b]items = [[/b][/size]
- [size=6][b]{"name": "Organic Bananas", "price": 399, "category": "Produce"},[/b][/size]
- [size=6][b]...[/b][/size]
- [size=6][b]][/b][/size]
- [size=6][b]discounts = [[/b][/size]
- [size=6][b]{"type": "item", "name": "Organic Bananas", "percent_off": 15},[/b][/size]
- [size=6][b]{"type": "category", "name": "Produce", "percent_off": 15},[/b][/size]
- [size=6][b]...[/b][/size]
- [size=6][b]][/b][/size]
- from itertools import combinations
- POINT_BUDGET = 20
- ITEM_DISCOUNT_PRICE = 2
- CATEGORY_DISCOUNT_PRICE = 5
- def get_discount_cost(discount):
- if discount["type"] == "item":
- return ITEM_DISCOUNT_PRICE
- return CATEGORY_DISCOUNT_PRICE
- def optimize_discounts(items, discounts):
- """
- Determine which discounts to activate to maximize savings.
- """
复制代码 |