1. 옷가게 할인받기
[내 풀이]
def solution(price) :
answer = 0
if price >= 500000 and price <= 1000000:
answer = int(price * 0.8)
elif price >= 100000 and price < 300000:
answer = int(price * 0.95)
elif price >= 300000 and price < 500000 :
answer = int(price * 0.9)
else:
answer = int(price)
return answer
경계라인에 있는 값도 잘 챙겨줘야한닷..
[다른 풀이]
(1)
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)
튜플을 사용한 풀이.. 너무 깔끔하다
튜플의 키값과 할인율을 순회하면서 price값이 key 값보다 크거나 같으면 계산해서 return
예를 들어 price = 45000원이면, 첫번째 요소는 패스하고 그 다음 요소에서 30000원 이상이므로 0.9 곱셈
2. 아이스 아메리카노
[내 풀이]
def solution(money):
answer = [] #max, change
price = 5500
max = 0
while money - price >= 0:
max += 1
money -= price
answer.append(max)
answer.append(money)
return answer
[다른 풀이]
(1)
def solution(money):
answer = [money // 5500, money % 5500]
return answer
(2)
def solution(money):
a, b = divmod(money, 5500)
return [a, b]
3. 나이 출력
[내 풀이]
def solution(age):
answer = 0
#current year = 2022
# birth year
# age = 2022 - birth + 1
answer = 2023 - age
return answer
[다른 풀이]
4. 배열 뒤집기
[내 풀이]
def solution(num_list):
answer = []
answer = reverse(num_list)
return answer
[다른 풀이]
(1)
def solution(num_list):
return num_list[::-1]
슬라이싱을 이용하깅 [a:b:c] (a부터 b앞까지 c 간격으로, c 음수이면 reverse)
(2)
def solution(num_list):
result =[]
while(num_list):
result.append(num_list.pop())
return result
pop으로 튀어나오는 값을 appen
(3) def solution(num_list):
num_list.reverse()
return num_list
.reverse() 메소드는 기존의 리스트를 변경한다
'개발 공부 일지 > Python' 카테고리의 다른 글
프로그래머스 코딩테스트 입문 day7 : 문자열, 조건문, 수학, 반복문 (0) | 2024.07.02 |
---|---|
프로그래머스 코딩테스트 입문 day6 : 문자열, 반복문, 출력, 배열, 조건문 (0) | 2024.07.02 |
프로그래머스 코딩테스트 입문 day4 : 수학 : 배열 (0) | 2024.06.23 |
파이썬 - 자릿수 지정하는 방법 (0) | 2024.06.22 |
프로그래머스 코딩테스트 입문 day3 : 사칙연산, 배열, 수학 (0) | 2024.06.22 |