코드 공부
[오늘의 코드 118] [프로그래머스] 옷가게 할인 받기
eun_00
2024. 9. 29. 22:00
https://school.programmers.co.kr/learn/courses/30/lessons/120818?language=python3
[문제]
- 머쓱이네 옷가게는 10만 원 이상 사면 5%, 30만 원 이상 사면 10%, 50만 원 이상 사면 20%를 할인해줍니다.
구매한 옷의 가격 price가 주어질 때, 지불해야 할 금액을 return 하도록 solution 함수를 완성해보세요.
[💡 정답]
import math
def solution(price):
answer = 0
if price < 100000:
answer = price
elif 100000 <= price < 300000:
answer = math.trunc(price * 0.95)
elif 300000 <= price < 500000:
answer = math.trunc(price * 0.9)
else:
answer = math.trunc(price * 0.8)
return answer
✔️ 알게된 것
- - math.trunc() : 정수부분만 반환
- floor() 내림, round() 반올림
- 다른 풀이를 보니 할인율 딕셔너리를 만들어서 한번에 작성하는 방법도 있었다.
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)