1. 최대로 할인 적용하기
Q.
다음과 같이 숫자로 이루어진 배열이 두 개가 있다.
하나는 상품의 가격을 담은 배열이고, 하나는 쿠폰을 담은 배열이다.
쿠폰의 할인율에 따라 상품의 가격을 할인 받을 수 있다.
이 때, 최대한 할인을 많이 받는다면 얼마를 내야 하는가?
단, 할인쿠폰은 한 제품에 한 번씩만 적용 가능하다.
shop_prices = [30000, 2000, 1500000]
user_coupons = [20, 40]
def get_max_discounted_price(prices, coupons):
# 이 곳을 채워보세요!
return 0
print("정답 = 926000 / 현재 풀이 값 = ", get_max_discounted_price([30000, 2000, 1500000], [20, 40]))
print("정답 = 485000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], [10, 70, 30, 20]))
print("정답 = 1550000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], []))
print("정답 = 1458000 / 현재 풀이 값 = ", get_max_discounted_price([20000, 100000, 1500000], [10, 10, 10]))
prices = [30000, 2000, 1500000]
coupons = [20, 40]
from collections import deque
def get_max_discounted_price(prices, coupons):
prices.sort()
coupons.sort()
prices_de = deque(prices)
coupons_de = deque(coupons)
count = 0
# 이 곳을 채워보세요!
while prices_de :
max_prices = prices_de.pop()
if coupons_de :
max_coupons = coupons_de.pop()
#int를 사용해서 소수점을 없앤다
count += int(max_prices - (max_prices * (max_coupons/100)))
else :
count += max_prices
return count
print("정답 = 926000 / 현재 풀이 값 = ", get_max_discounted_price([30000, 2000, 1500000], [20, 40]))
print("정답 = 485000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], [10, 70, 30, 20]))
print("정답 = 1550000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], []))
print("정답 = 1458000 / 현재 풀이 값 = ", get_max_discounted_price([20000, 100000, 1500000], [10, 10, 10]))
2. 괄호열고 닫기
Q. 괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻이다. 예를 들어
()() 또는 (())() 는 올바르다.
)()( 또는 (()( 는 올바르지 않다.
이 때, '(' 또는 ')' 로만 이루어진 문자열 s가 주어졌을 때, 문자열 s가 올바른 괄호이면 True 를 반환하고 아니라면 False 를 반환하시오.
def is_correct_parenthesis(string):
stack = []
for i in range(len(string)):
if string[i] == "(":
stack.append("(") # 여기 아무런 값이 들어가도 상관없습니다! ( 가 들어가있는지 여부만 저장해둔 거니까요
elif string[i] == ")":
if len(stack) == 0:
return False
stack.pop()
if len(stack) != 0:
return False
else:
return True
print("정답 = True / 현재 풀이 값 = ", is_correct_parenthesis("(())"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis(")"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())))"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("())()"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())"))
3. 멜론 베스트 앨범 뽑기
Q. 멜론에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 한다.
노래는 인덱스 구분하며, 노래를 수록하는 기준은 다음과 같다.
1. 속한 노래가 많이 재생된 장르를 먼저 수록한다. (단, 각 장르에 속한 노래의재생 수 총합은 모두 다르다.)
2. 장르 내에서 많이 재생된 노래를 먼저 수록한다.
3. 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록한다.
노래의 장르를 나타내는 문자열 배열 genres와
노래별 재생 횟수를 나타내는 정수 배열 plays가 주어질 때,
베스트 앨범에 들어갈 노래의 인덱스를 순서대로 반환하시오.
from collections import defaultdict
def best_album(genres, plays):
# 1. 장르별 총 재생 횟수 계산
genre_play_counts = defaultdict(int)
songs_by_genre = defaultdict(list)
for i, (genre, play) in enumerate(zip(genres, plays)):
genre_play_counts[genre] += play
songs_by_genre[genre].append((play, i))
# 2. 장르별 우선순위 정렬
sorted_genres = sorted(genre_play_counts.items(), key=lambda x: x[1], reverse=True)
# 3. 장르별 노래 정렬 및 베스트 앨범 구성
result = []
for genre, _ in sorted_genres:
# 해당 장르의 노래를 재생 수와 인덱스로 정렬
songs = sorted(songs_by_genre[genre], key=lambda x: (-x[0], x[1]))
# 최대 두 곡을 베스트 앨범에 추가
result.extend([song[1] for song in songs[:2]])
return result
# 테스트
genres1 = ["classic", "pop", "classic", "classic", "pop"]
plays1 = [500, 600, 150, 800, 2500]
print("정답 = [4, 1, 3, 0] / 현재 풀이 값 =", best_album(genres1, plays1))
genres2 = ["hiphop", "classic", "pop", "classic", "classic", "pop", "hiphop"]
plays2 = [2000, 500, 600, 150, 800, 2500, 2000]
print("정답 = [0, 6, 5, 2, 4, 1] / 현재 풀이 값 =", best_album(genres2, plays2))
이건 절대 못풀거같다...
'코딩테스트' 카테고리의 다른 글
하루 코테 1개 풀기 - 1일차 (0) | 2025.02.25 |
---|---|
하루 코테 3개 풀기 - 19일차 (1) | 2024.12.29 |
하루 코테 3개 풀기 - 17일차 (1) | 2024.12.28 |
하루 코테 3개 풀기 - 16일차 (0) | 2024.12.28 |
하루 코테 3개 풀기 - 15일차 (0) | 2024.12.24 |