happy coding

[level 0] 옷가게 할인 받기 본문

coding study/programmars

[level 0] 옷가게 할인 받기

yeoonii 2024. 5. 25. 17:21

문제 설명

머쓱이네 옷가게는 10만 원 이상 사면 5%, 30만 원 이상 사면 10%, 50만 원 이상 사면 20%를 할인해줍니다.
구매한 옷의 가격 price가 주어질 때, 지불해야 할 금액을 return 하도록 solution 함수를 완성해보세요.


제한사항
  • 10 ≤ price ≤ 1,000,000
    • price는 10원 단위로(1의 자리가 0) 주어집니다.
  • 소수점 이하를 버린 정수를 return합니다.
import math

def solution(price):
    answer = price
    if (price >= 100000) :
        answer = math.trunc(price * 0.95)
    if (price >= 300000) :
        answer = math.trunc(price * 0.9)
    if (price >= 500000):
        answer = math.trunc(price * 0.8)
    return answer

 

다른 사람 풀이

def solution(price):
    discount_rates = {500000: 0.8, 300000: 0.9, 100000: 0.95, 0: 1}
    for discount_price, discount_rate in discount_rates.items():
        if price >= discount_price:
            return int(price * discount_rate)

'coding study > programmars' 카테고리의 다른 글

[level 0] 개미 군단  (0) 2024.05.25
[level 0] 아이스 아메리카노  (0) 2024.05.25
[level 0] 짝수는 싫어요  (0) 2024.05.25
[level 0] 중앙값 구하기  (0) 2024.05.25
[level 0] 겹치는 선분의 길이  (0) 2024.05.06
Comments