【问题标题】:Buy any X product for X amount?以 X 金额购买任何 X 产品?
【发布时间】: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


【解决方案1】:

这里有一个快速模板,可以帮助您开始解决这个问题: [注意:使用 set() 快速获取项目差异,并使用 print() 语句确认每个步骤都是预期的] 同样,这 不是 一个完整的解决方案 - 但只是提供了一个很好的模板让你快速开始。

from pprint import pprint

lowest_price_items  = ['tshirt', 'shorts']
discount_price_items = ['shirt', 'pants']
discount_Set = set(discount_price_items)

cart = ['shirt', 'shirt', 'shirt', 'shirt', 'shirt', 'pants', 'pants', 'pants', 'pants', 'tshirt', 'shorts']
cart_Set = set(cart)
low_price_goods =  cart_Set - discount_Set


pprint(product)

print(f' products: {product.keys()} ') #  first level of prod. dict.
print(product['shirt'].keys())         #  'price'  and 'no_requied_for_dis'

#       products   key1                key2
print(product['shirt']['no_reqired_for_dis']['discount_price'])  # 250
tshirt_price = product['tshirt']['price']
print(tshirt_price)

"""
total should be 250 * 3 + remaining item * lowest_price_products (tshirts, shorts) only
"""

total_items = len(cart)
print(total_items)

# modify this to calculate the final price.
if total_items > 3:
    final_price = 250 * total_items %3 + "remaining item * lowest_price_products" # select the lowest price items

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2011-12-10
    相关资源
    最近更新 更多