알고리즘

[프로그래머스 Lv1 - 2019 KAKAO BLIND RECRUITMENT] 실패율 (Python)

lwkejpf 2022. 6. 30. 08:00

 


 

def solution(N, stages):
    length = len(stages)
    answer = []
    fail = dict()
    
    for i in range(1, N+1) :
        cnt = stages.count(i)
        val = cnt / length
        fail[i] = val
        length -= cnt
        
    fail = sorted(fail.items(), key=lambda x:x[1], reverse=True)
    for i in fail :
        answer.append(i[0])
    return answer

 

 

ㅎㅎ.. 웬일로 쉽게 끝나나 했다 ,,

 


 

def solution(N, stages):
    length = len(stages)
    answer = []
    fail = dict()
    
    for i in range(1, N+1) :
        cnt = stages.count(i)
        if cnt == 0 :
            fail[i] = 0
            continue
        else :
            val = cnt / length
            fail[i] = val
            length -= cnt

    fail = sorted(fail.items(), key=lambda x:x[1], reverse=True)
    for i in fail :
        answer.append(i[0])
    return answer

 

통과 빠르게 뜰 때마다

 

..? 이게 .. 된다고?

 

바꿔준거라곤 cnt 0 일 때

바로 0 때려주고 continue 시켜준 것밖에 없는데 ..

 

 

맞다 맨날 까먹는거 정리해놔야지

 

1) 딕셔너리 Key 를 기준으로 정렬하기

sorted(d) 

- 기본적으로 sorted 사용하면 key 를 기준으로 정렬되는데,

리스트형으로 반환됨.

 

2) 딕셔너리 Value 를 기준으로 정렬하기

sorted(d, key=lambda x : x[1])

 

3) items() 이용하여 정렬하기

sorted(d.items(), key=lambda x : x[0]) 

sorted(d.items(), key=lambda x : x[1])

- 여기서 x[0] 은 Key, x[1] 은 Value 의미

 

4) 내림차순 정렬하기

마찬가지로 'reverse=True' 추가해주면 됨.