coding-coworking-club / basic-python-fall-2021

11 stars 6 forks source link

[Homework 2] 省錢小作戰 #207

Open CerebellarTentorium opened 3 years ago

CerebellarTentorium commented 3 years ago

提交連結

https://judge.ccclub.io/status/4e2603e3de3ad6b9957e8512e3fdd495

程式碼

shop_list = []
for num in range(4):
    shop = input().split()
    shop_list.append(shop)
shop_dict = {"S商店": shop_list[0], 
             "C商店": shop_list[1],
             "P商店": shop_list[2],
             "M商店": shop_list[3]
             }
#將輸入之商店資料整理成list後放入以商店名為key的字典

def calculate_money(shop_data):#建立一個計算價錢的函數
    price = 0
    discount = 100
    delivery_fee = 0
    rebate = 0
#建立各項參數:價錢、折扣、運費、回饋%數

    if shop_data[0] == "0":
        return float("inf")
#若貨量為零,回傳無限,這樣最後放入價錢list後再sort就會排到最後

    else:
        price = float(shop_data[1])
#貨量不為零給定price值

    if "%" in "".join(shop_data):
        rebate = float(shop_data[2].replace("%", ""))
    else:
        rebate = 0
#偵測到有關折扣的敘述就給予rebate折扣值

    if "免" in "".join(shop_data):
        delivery_fee = 0
    elif "%" in "".join(shop_data):
        delivery_fee = float(shop_data[3].replace("運費", ""))
    else:
        delivery_fee = float(shop_data[2].replace("運費", ""))
#偵測到有關運費的敘述就給予delivery_fee運費值

    if "打" in "".join(shop_data):
        all_discount = shop_data[-1].replace("滿", " ").replace("打", " ").replace("折", " ").replace(",", " ")
        all_discount_list = all_discount.split()#將去除中字之數字字串放入list
        discount_threshold = all_discount_list[0::2]
        discount_amount = all_discount_list[1::2]#將打折值與需花費的金額分開成兩個list
        discount_threshold_num = [float(num) for num in discount_threshold]#要獲得折扣所需花費金額
        discount_amount_num = [float(num) for num in discount_amount]#折扣值
        discount_amount_num_new = []
        for num in discount_amount_num:#將折扣值全部換成二位數
            if num // 10 < 1:
                discount_amount_num_new.append(num * 10)
            else:
                discount_amount_num_new.append(num)

        discount_threshold_num.append(price)
        discount_threshold_num_sorted = sorted(discount_threshold_num)
        index_of_price = discount_threshold_num_sorted.index(price)
        if index_of_price != 0:
            the_threshold_price = discount_threshold_num_sorted[index_of_price - 1]
            i = discount_threshold_num.index(the_threshold_price)
            discount = discount_amount_num_new[i]
#找到折扣值

        else:
            discount = 100
    else:
        discount = 100
#偵測到關鍵字給予discount折扣值

    money = (( price * discount / 100 ) + delivery_fee) * (100 - rebate) / 100 #將以獲得的參數丟到公式計算
    return money

shop_dict["S商店"] = calculate_money(shop_dict["S商店"])#將字典中每個商店的資料以定義好的函式計算商品價格
shop_dict["C商店"] = calculate_money(shop_dict["C商店"])
shop_dict["P商店"] = calculate_money(shop_dict["P商店"])
shop_dict["M商店"] = calculate_money(shop_dict["M商店"])
money_dict_values = list(shop_dict.values())
money_list = [float(num) for num in money_dict_values]
money_list.sort()
the_cheapest = money_list[0]
lst = [list(shop_price) for shop_price in shop_dict.items()]
for items in lst:
    if items[1] == the_cheapest:
        print(items[0] + " " + str(round(items[1])))
        break

錯誤訊息

Wrong Answer

問題描述

  1. 我目前的想法是將資料先以字典整理好後,寫一個函式,而函試會將需要的參數依據獲得的資料附值之後丟入公式計算出商品價格。將各商店的商品價格比較之後獲得最便宜的價格再將價格與商店名稱一同印出。
  2. 請問會出現四間商店貨量全都為零的情況嗎?因為題幹中沒有描述出現這個情況時我的答案應該輸出什麼結果。
  3. 請問題幹設計若出現測資為價錢越高折扣越少,應該依貨價所到的折扣級距為主還是最划算的折扣為主?我目前是以貨價所到的級距去撰寫答案。
  4. 目前遇到的問題是以judge的兩項測資測試之外也有自己設計一些測資去測試,也有考慮價錢為零的情況也可將價錢為零的商店印出,基本上除了所有貨物都是零的情況無法打印出結果以外都可以輸出正確答案,想詢問助教我還有什麼思考盲點或是我的答案是何種測資測試錯誤?

謝謝助教協助。

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.