https://school.programmers.co.kr/learn/courses/30/lessons/161989
from collections import Counter
def solution(N, stages):
answer = []
_dict = Counter(stages)
_list = [[i,-1] for i in range(1, N+1)]
persons = len(stages)
for i in range(N):
temp = _dict[i+1]
_list[i][1]= temp/persons
persons -= temp
_list.sort(key = lambda x:(x[1],-x[0]),reverse = True)
for l in _list:
a,b= l
answer.append(a)
return answer
코드를 돌릴때 딱히 문제가 없어서 간과하고 있었던 부분이 있다. 실패율을 구할때 0으로 나눈 경우가 있을 수 있다.
ZeroDivisionError: division by zero
이부분을 조건문으로 달아야 예외처리 해야한다.
from collections import Counter
def solution(N, stages):
answer = []
_dict = Counter(stages)
_list = [[i,-1] for i in range(1, N+1)]
persons = len(stages)
for i in range(N):
temp = _dict[i+1]
if temp != 0 and persons != 0:
_list[i][1]= temp/persons
persons -= temp
_list.sort(key = lambda x:(x[1],-x[0]),reverse = True)
for l in _list:
a,b= l
answer.append(a)