Lumiere, and s5unnyjjj

[홀로 하는 코딩 공부] 이모티콘 할인행사(Python) 본문

Algorithm/Python

[홀로 하는 코딩 공부] 이모티콘 할인행사(Python)

s5unnyjjj 2024. 5. 1. 19:53
반응형

프로그래머스

사용 언어: Python

문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

*** 본 문제를 푸는 과정을 공유하려 한다.


▶ 할인율이 10,20,30,40으로 고정되어있기에 할인율 조합 후보군을 모두 생성한 다음, 각각의 후보에 대해서 계산하여 조건에 맞는 답을 리턴하도록 하였다. 할인율이 고정되어있기에 쉬운 문제인 것 같다. 코드는 아래와 같다.

def solution(users, emoticons):
    answer = []
    all_discounts = [10, 20, 30, 40]
    cand_discounts = []

    def dfs(ary_discnts, depth):
        if len(emoticons) == depth: 
            cand_discounts.append(ary_discnts[:])
        else:
            for discount in all_discounts:
                ary_discnts[depth] = discount
                dfs(ary_discnts, depth+1)
                ary_discnts[depth] = 0
            
    dfs([0]*len(emoticons), 0)

    for cur_discount in cand_discounts:
        cur_emoticons = [int(price*(1-discount*0.01)) for price, discount in zip(emoticons, cur_discount)]
        cur_answer = [0, 0]
        for user in users:
            user_discount, user_max_price = user
            user_total = 0
            for i, per_discount in enumerate(cur_discount):
                if user_discount <= per_discount:
                    user_total += cur_emoticons[i]
            if user_max_price <= user_total:
                cur_answer[0] += 1
            else:
                cur_answer[1] += user_total
        if sum(answer) == 0:
            answer = cur_answer
        else:
            if answer[0] == cur_answer[0]:
                if answer[1] < cur_answer[1]:
                    answer = cur_answer
            elif answer[0] < cur_answer[0]:
                answer = cur_answer
                
    return answer

 

 

▶ 3개의 테스트 케이스에 대해서 틀린다고 나온다. 원인을 파악하기 위해 고려하지못한 조건이 있는지 문제를 다시 읽어보았다. 하지만 없었다. 그래서 코드를 다시 봤다. 크게 이상한 부분이 없었는데 혹시 할인율이 적용된 가격이 담긴 cur_emoticons 배열에서 가격을 저장할 때, int로 변환하여 저장하게되면서 소수점 값이 없어져버린게 문제가 아닐까 생각하여 해당 부분을 변환해보았다.

def solution(users, emoticons):
    answer = []
    all_discounts = [10, 20, 30, 40]
    cand_discounts = []

    def dfs(ary_discnts, depth):
        if len(emoticons) == depth: 
            cand_discounts.append(ary_discnts[:])
        else:
            for discount in all_discounts:
                ary_discnts[depth] = discount
                dfs(ary_discnts, depth+1)
                ary_discnts[depth] = 0
            
    dfs([0]*len(emoticons), 0)

    for cur_discount in cand_discounts:
        cur_emoticons = [price*(1-discount*0.01) for price, discount in zip(emoticons, cur_discount)]
        cur_answer = [0, 0]
        for user in users:
            user_discount, user_max_price = user
            user_total = 0
            for i, per_discount in enumerate(cur_discount):
                if user_discount <= per_discount:
                    user_total += cur_emoticons[i]
            if user_max_price <= user_total:
                cur_answer[0] += 1
            else:
                cur_answer[1] += user_total
        if sum(answer) == 0:
            answer = cur_answer
        else:
            if answer[0] == cur_answer[0]:
                if answer[1] < cur_answer[1]:
                    answer = cur_answer
            elif answer[0] < cur_answer[0]:
                answer = cur_answer
                
    return answer

 

 성공이다. 난이도가 높은 문제는 아니었던 것 같다.


>> 위 내용은 필자가 코딩테스트 문제를 푼 코드입니다.

>> 부족한 점이 많을 수 있기에 잘못된 내용이나 궁금한 사항이 있으면 댓글 달아주시기 바랍니다.

>> 긴 글 읽어주셔서 감사합니다. 

반응형
Comments