Last active
March 16, 2016 14:03
-
-
Save momotaro98/2c7d9e2c56600b46b1cc to your computer and use it in GitHub Desktop.
Voyage Group "CTOからの挑戦状2015_2nd" Level3の解答
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MENU = { | |
"ジェノベーゼM": 1000, | |
"ジェノベーゼL": 1400, | |
"マルゲリータM": 1200, | |
"マルゲリータL": 1800, | |
"ポテトフライ": 400, | |
"グリーンサラダ": 500, | |
"シーザーサラダ": 600 | |
} | |
PIZZA = [ | |
"ジェノベーゼM", | |
"ジェノベーゼL", | |
"マルゲリータM", | |
"マルゲリータL" | |
] | |
SIDE = [ | |
"ポテトフライ", | |
"グリーンサラダ", | |
"シーザーサラダ" | |
] | |
ORDINARY_DAY = [ | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thurseday", | |
"Friday" | |
] | |
# セットメニュー割引料金を求める | |
def findSetDisc(order, day, time): | |
L2pizza = 0 | |
pizza = 0 | |
side_order = [] | |
for key, value in order.items(): | |
if key in ["ジェノベーゼL", "マルゲリータL"]: | |
L2pizza += value | |
if key in PIZZA: | |
pizza += value | |
if key in SIDE: | |
side_order.append(key) | |
# ピザL2セットが適応するとき | |
if L2pizza >= 2 and side_order: | |
if "シーザーサラダ" in side_order: | |
return 600 | |
if "グリーンサラダ" in side_order: | |
return 500 | |
if "ポテトフライ" in side_order: | |
return 400 | |
# ピザ2セットが適応するとき | |
if pizza >= 2 and "ポテトフライ" in side_order: | |
return 400 | |
# 時刻の分基準の値を求める | |
time_list = [int(i) for i in time.split(":")] | |
time_minute = time_list[0]*60 + time_list[1] | |
# 平日ランチセットが適応するとき | |
if day in ORDINARY_DAY and time_minute >= 660 and time_minute <= 840 and pizza and side_order: | |
return 400 | |
# どのセットにも適応しないとき | |
return 0 | |
# オーダーの合計を求める | |
def findAmount(order): | |
amount = 0 | |
for key, value in order.items(): | |
amount += MENU[key] * value | |
return amount | |
# 利用できるクーポンの枚数と残りの支払い金額を求める | |
def determinHowManyUse(amount, coupon_num, coupon_disc): | |
for num in range(coupon_num, -1, -1): | |
if num * coupon_disc <= amount: | |
coupon_num = num | |
break | |
return coupon_num, amount - coupon_disc*coupon_num | |
def selectOptimumCombination(myOrder, myCoupons, day, time): | |
""" | |
# Python DocString Test | |
入力, 出力のリストについて | |
[500円クーポンの枚数, 200円クーポンの枚数, 100円クーポンの枚数, 400円ピザクーポンの枚数] | |
# Case1 クーポン使った方がお得なとき | |
>>> selectOptimumCombination({"ジェノベーゼM": 1, "マルゲリータM": 1, "ポテトフライ": 1}, [1, 0, 0, 0], "Saturday", "11:00") | |
[1, 0, 0, 0] | |
# Case2 セットの方がお得なとき | |
>>> selectOptimumCombination({"ジェノベーゼL": 1, "マルゲリータL": 1, "シーザーサラダ": 1}, [1, 0, 0, 0], "Saturday", "11:30") | |
[0, 0, 0, 0] | |
# Case3 平日割引を使った方がお得なとき | |
>>> selectOptimumCombination({"マルゲリータL": 1, "シーザーサラダ": 1}, [0, 1, 0, 0], "Wednesday", "11:30") | |
[0, 0, 0, 0] | |
""" | |
# 注文の合計金額を求める | |
amount = findAmount(myOrder) | |
# 合計が1000円以下ならばクーポン利用不可で終了 | |
if amount <= 1000: | |
return [0, 0, 0, 0] | |
# 各クーポンの制限枚数を考慮した利用できる最大枚数を求める | |
maxUse500 = min(myCoupons[0], 2) | |
maxUsePizza400 = min(myCoupons[3], 1) | |
maxUse200 = min(myCoupons[1], 2) | |
maxUse100 = min(myCoupons[2], 3) | |
# クーポンを使った場合のクーポンの枚数と支払い金額を求める | |
coupon_pay = amount | |
use500, coupon_pay = determinHowManyUse(coupon_pay, maxUse500, 500) | |
if [order for order in myOrder.keys() if order in PIZZA]: | |
usePizza400, coupon_pay = determinHowManyUse(coupon_pay, maxUsePizza400, 400) | |
else: | |
usePizza400, coupon_pay = 0, coupon_pay | |
use200, coupon_pay = determinHowManyUse(coupon_pay, maxUse200, 200) | |
use100, coupon_pay = determinHowManyUse(coupon_pay, maxUse100, 100) | |
# セットを適応したときの支払い金額を求める | |
set_pay = amount - findSetDisc(myOrder, day, time) | |
# クーポン値引きとセット値引きでどちらがお得かを比較 | |
if set_pay <= coupon_pay: | |
use500, use200, use100, usePizza400 = 0, 0, 0, 0 | |
return [use500, use200, use100, usePizza400] | |
# doctest main | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment