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' 추가해주면 됨.
'알고리즘' 카테고리의 다른 글
[프로그래머스 Lv1 - 2020 카카오 인턴십] 키패드 누르기 (Python) (0) | 2022.06.30 |
---|---|
[프로그래머스 Lv1 - 2021 카카오 채용연계형 인턴십] 숫자 문자열과 영단어 (Python) (0) | 2022.06.30 |
[프로그래머스 Lv1 - 2021 KAKAO BLIND RECRUITMENT] 신규 아이디 추천 (Python) (0) | 2022.06.30 |
[프로그래머스 Lv1 - 2022 KAKAO BLIND RECRUITMENT] 신고 결과 받기 (Python) (0) | 2022.06.28 |
[프로그래머스 Lv1] K번째 수 (Python) (0) | 2022.06.27 |