프로그래머스_LV.0_옷가게 할인 받기

Dreamer ·2022년 12월 9일
0

코딩테스트 준비

목록 보기
28/93

1. quiz

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

2. answer

def solution(price):
    if price >= 500000:
        return int(price*0.8)
    elif price >= 300000 and price < 500000:
        return int(price * 0.9)
    elif price >= 100000 and price < 300000:
        return int(price * 0.95)
    else:
        return int(price)

3. 다른 사람의 풀이

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)
  • 깔끔한 코드라고 생각된다.
  • 앞으로 if, elif 구문을 사용하기 전에 {} 딕셔너리에 값을 넣고, 깔끔하게 값을 전달 받는 걸 고민해봐야겠다.
profile
To be a changer who can overturn world

0개의 댓글