코테일지
알고리즘 with python.06
hatso
2021. 6. 13. 11:36
쓱 최대로 할인 적용하기
코드
버블,선택,삽입 정렬을 이해한 우리는 파이썬에서 제공하는 .sort함수를 사용할 자격이 있다.
배열을 reverse=True를 이용해 내림차순으로 정렬한다.
인덱스 값을 활용하여 할인율 적용.
shop_prices = [30000, 2000, 1500000]
user_coupons = [20, 40]
def get_max_discounted_price(prices, coupons):
shop_prices.sort(reverse=True)
print(shop_prices)
user_coupons.sort(reverse=True)
print(user_coupons)
price_index = 0
coupon_index = 0
max_discounted_price = 0
while price_index < len(prices) and coupon_index < len(coupons):
max_discounted_price += prices[price_index] * (100 - coupons[coupon_index]) / 100
price_index += 1
coupon_index += 1
while price_index < len(prices):
max_discounted_price += prices[price_index]
price_index += 1
return max_discounted_price
print(get_max_discounted_price(shop_prices, user_coupons)) # 926000 이 나와야 합니다.