반응형 코테46 Python 옷가게 할인 받기 [코딩테스트 입문 - Day 5] 문제 구현 if 조건문으로 구간을 나눠 계산한 값을 리턴해준다. 코드 def solution(price): if price>=500000: return int(price*0.8) elif price>=300000: return int(price*0.9) elif price>=100000: return int(price*0.95) else: return price 다른 풀이 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(pri.. 2023. 2. 6. Python 나이 출력 [코딩테스트 입문 - Day 5] 문제 구현 현재 연도에서 나이를 빼고 +1을 해준 값을 리턴해준다. 코드 def solution(age): return 2022-age+1 다른 풀이 이 문제에는 더 나은 풀이가 없다. 2023. 2. 6. Python 아이스 아메리카노 [코딩테스트 입문 - Day 5] 문제 구현 돈을 5500으로 나눈 값과 mod 한 값을 리턴하면 된다. 코드 def solution(money): return [money//5500, money%5500] 다른 풀이 def solution(money): return divmod(money, 5500) divmod를 써서 한 번에 리턴해줘도 된다. 2023. 2. 6. Python 배열의 평균값 [코딩테스트 입문 - Day4] 문제 구현 배열의 합을 배열의 길이로 나눠서 리턴해준다. 코드 def solution(numbers): return sum(numbers)/len(numbers) 다른 풀이 def solution(numbers): sum1 = 0 for i in numbers: sum1 += i answer = sum1/len(numbers) return answer 정석적인 풀이는 다음과 같다. 배열을 돌면서 sum값을 구해주고 그 값을 배열의 길이로 나눠주면 된다. 2023. 1. 17. 이전 1 ··· 4 5 6 7 8 9 10 ··· 12 다음 반응형