2019 카카오 신입 공채 1차: 2. 실패율 (Python)

4440 단어 kakaokakao

실패율

  • Difficulty: Level 1
  • Type: Array/Sorting
  • link

Solution

  • collections 의 Counter 를 적절히 활용해 실패 확율을 찾으면 결과 값들을 쉽게 정렬할 수 있다
import collections
def solution(N, stages):
    # Count each occurrence
    counter = collections.Counter(stages)
    # Dict to save fail rate
    failure = collections.defaultdict(float)
    keys = list(counter.keys())
    for i in range(1,N+1):
        # Total sum of occurence for stages greater or equal
        total = sum([counter[k] if k>= i else 0 for k in keys])
        # Exception handling for zero division
        if total == 0:
            failure[i] = 0
        else:
            failure[i] = counter[i] / total

    # Sort result by failure rate in descending order
    ans = sorted(list(range(1,N+1)),key=lambda x: failure[x],reverse=True)
    return ans

좋은 웹페이지 즐겨찾기