실패율

발자·2022년 9월 12일
0

programmers

목록 보기
8/34

python

def solution(N, stages):
    # 스테이지 오름차순 정렬
    stages.sort()

    # 스테이지별 클리어 현황
    clearStage = [0]*(N+1)
    for i in stages:
        clearStage[i-1] += 1

    # 스테이지별 실패율
    failureRate = {}
    for i in range(0, N) :
        if sum(clearStage[i:]) == 0 :
            failureRate[i+1] = 0
        else :
            failureRate[i+1] = clearStage[i]/sum(clearStage[i:])

    # 실패율로 내림차순 정렬
    sorted_failureRate = dict(sorted(failureRate.items(), key=lambda x : x[1], reverse=True))

    answer = list(sorted_failureRate.keys())
    return answer

🗝️ dict 정렬
🗝️ dict keys(), values()
🧩 스테이지별 클리어 현황을 거치지 않고 바로 실패율을 구하면 더 깔끔하다.
🧩 실패율로 내림차순 정렬할 때 items()를 이용하지 않으면 한 줄로 줄일 수 있다.

return sorted(failureRate, key=lambda x : failureRate[x], reverse=True)

0개의 댓글