【发布时间】:2021-03-29 14:32:10
【问题描述】:
我试图解决这个问题,但我无法弄清楚。
我有product 字典:
product = {
"shirt" : {
"price" :300 ,
"no_reqired_for_dis" : {"3": ["shirt","pents","tshirt","shorts"],"discount_price" : 250}},
"pents" : {
"price" :200 ,
"no_reqired_for_dis" : {"3": ["shirt","pents","tshirt","shorts"],"discount_price" : 250}}
"tshirt" : {
"price" :150 ,
"no_reqired_for_dis" : {"3": ["shirt","pents","tshirt","shorts"],"discount_price" : 250}}
"shorts" : {
"price" :100 ,
"no_reqired_for_dis" : {"3": ["shirt","pents","tshirt","shorts"],"discount_price" : 250}}
}
找出总数的最佳方法应该是什么 如果任何人购买至少三种产品或 3 的倍数,他们将以 250 的价格获得 3 件商品的折扣标准?
例如,如果有人购买了总共 11 件(衬衫 = 5,裤子 = 4,T 恤 = 1,短裤 = 1)产品,那么他们的总数应该是 250 * 3 + 剩余物品 * 最低价格的产品。这里剩余的项目应该是产品的最低价格(这里应该是短裤和T恤)。
我已经这样做了:
total_payment = 0
total_product = {"shirt" : 5,"pents":4,"tshirt":1,"shorts" 1}
total_item = sum(total_product.values())
for key, value in total_product.items():
min_no_required_for_discount = product[key]["no_required_for_dis"].keys()
if total_item < int(min_no_required_for_discount[0]:
total_payment += value * product[key]["price"]
else:
remaining_unit = total_item % 3
total_pair = (total_item - remaining_unit) // 3
total_payment += total_pair * 250
现在我对剩余单元感到困惑。如何计算剩余单位的价格,因为剩余单位必须乘以最低价格。在上面的示例中,remaining_unit 将为 2,它将计算短裤和 tshirt 的价格
【问题讨论】:
-
所以基本上你想忽略所有项目的价格,除了
len(cart) % 3最便宜的(假设cart是像["shirt", "shirt", "shorts"]这样的项目列表)——试着把一个排序的价格列表,然后根据该信息仅考虑您关心的价格。 -
@alex - 你能告诉我们你卡在哪里了吗?以及您尝试过的最初努力?
-
@entrez 所以基本上我想尝试的是计算总金额,所以我在这里尝试计算最大折扣。
-
嗨,@alex,希望对您有所帮助。
-
嗨,丹尼尔,我刚刚通过初步努力更新了原始帖子。
标签: python python-3.x python-2.7 dictionary