본문 바로가기

스크립트

[코딩테스트 입문] 옷가게 할인 받기

반응형

옷가게 할인 받기

1안) solution.py

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

2안) solution.py

import math

def solution(price):
    answer = 0
    
    if price >= 500000:
        answer = price - price * 20 / 100
    elif price >= 300000:
        answer = price - price * 10 / 100
    elif price >= 100000:
        answer = price - price * 5 / 100
    else:
        answer = price
    answer = math.trunc(answer)
    
    return answer

출처 - 프로그래머스(코딩테스트 연습) : https://school.programmers.co.kr/learn/courses/30/lessons/120818


python math.trunc() method

math.trunc() 메서드는 숫자의 잘린 정수 부분을 반환합니다.(단순히 소수점을 제거)

# Import math Library
import math

# Return the truncated integer parts of different numbers
print(math.trunc(2.77))
print(math.trunc(8.32))
print(math.trunc(-99.29))
2
8
-99

 

참고URL

- w3schools : https://www.w3schools.com/python/ref_math_trunc.asp

 

728x90
반응형