스파르타 코딩 클럽 알고리즘 3주차 숙제

Justin. S. Hong·2021년 8월 9일
0

스파르타코딩클럽

목록 보기
3/11

이번주 참고사항
1. 1번 숙제는 조금 바꿔서 작성해봄(강의 자료의 로직을 다른 방식으로 구현해봄)

숙제하면서 알게 된 것
1. if-else 문 중 if 부분에서 pass를 사용할 경우 if문에서만 빠져나옴

숙제 내용
1. 쿠폰을 사용하여 ssg 최대 할인 적용
2. 올바른 괄호
3. 멜론 베스트 앨범 뽑기(추후에 작성 예정)

1번 문제

shop_prices = [30000, 2000, 1500000]
user_coupons = [20, 40]

위와 같이 가격 및 할인율이 설정되어 있다고 가정할 경우 최대 할인은 아래와 같이 적용할 수 있다.

def get_max_discounted_price(prices, coupons):
    prices.sort() # 가격만 오름차순으로 정렬
    price_index = 0
    coupon_index = 0
    max_discounted_price = 0

    while price_index < len(prices) and coupon_index < len(coupons):
    	# 만약 가격 배열에서 2000원을 가리킬 경우
        # 그냥 가격만 더하고 가격 배열의 다음 인덱스만 증가
        if price_index == 0:
            max_discounted_price += prices[price_index]
            price_index += 1
            pass
        # 아닌 경우 할인율이 가장 적은 쿠폰부터 사용
        # 단, 쿠폰 할인율도 오름차순으로 정렬되어야 함
        else:
            max_discounted_price += prices[price_index] * (100 - coupons[coupon_index]) / 100
            price_index += 1
            coupon_index += 1
	
    while price_index < len(prices):
        if price_index == 0:
            price_index += 1
            pass
        else:
            max_discounted_price += prices[price_index]
            price_index += 1

    return max_discounted_price

2번 문제

def is_correct_parenthesis(string):
    stack = []

    for i in range(len(string)):
        if string[i] == "(":
        	# 스택에 '('를 저장
            stack.append(i)
        # 스택에 ')'가 들어오면 '('도 뽑음
        elif string[i] == ")":
        	# 단, 스택이 빌 경우 False를 반환
            if len(stack) == 0:
                return False
            stack.pop()

	# '(' 및 ')'를 모두 뽑아도 남은 경우 False 반환
    if len(stack) != 0:
        return False
    # 모두 뽑고 빈 경우 True 반환
    else:
        return True

문제 3번

profile
KOSA -> 오즈코딩스쿨을 거쳐 프론트엔드 개발자로 성장하기 ESTJ-1W2

0개의 댓글