일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- Github
- WebOS
- D3
- 다시풀기
- py
- level3
- 대학원
- 자바
- 대학원일기
- 어렵다
- 휴학
- 컨트리뷰톤
- LEVEL2
- 프로그래머스
- 컴퓨터비전
- SWEA
- level4
- BFS
- java
- level1
- SQL
- 내휴학생활중의아주큰일
- 파이썬
- git
- MSBuild
- androidstudio
- build
- 안드로이드스튜디오
- Matrix Factorization
- Python
Archives
- Today
- Total
bit가 눈 앞에서 왔다갔다
Py) 프로그래머스 42579 본문
https://programmers.co.kr/learn/courses/30/lessons/42579
코딩테스트 연습 - 베스트앨범
스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가
programmers.co.kr
def solution(genres, plays):
answer = []
dict = {}
for i in range(len(genres)):
if genres[i] in dict:
dict[genres[i]][0].append(i)
dict[genres[i]][1] += plays[i]
else:
dict[genres[i]] = [[i], plays[i]]
#print(dict)
dict_list = sorted(dict.items(), key=lambda x: -x[1][1])
# dict.items를 정렬할건데, lambda를 사용할것.: value의 [1]인덱스 사용
#print(dict_list)
for j in dict_list:
if len(dict[j[0]][0]) == 1:
answer.append(dict[j[0]][0][0])
else:
dict[j[0]][0].sort(key=lambda x: -plays[x])
answer.extend(dict[j[0]][0][:2])
return answer
정말..헷갈리는 코드였다...
버리지 않고 계속 붙잡고 있었던건.. 이중 딕셔너리? 딕셔너리를 공부하기 위함 때문이다.. 그거 아니었으면 진작 버림
그와중에 lambda에서 -plays[x] 부분은 이해가 안됨 저게 뭐지
*
lambda : dictionary의 x[0]은 key를, x[1]는 value를 의미함 / - 붙이면 역순
append, extend 차이
extend : iterable 자료형에서 사용 -> https://ooyoung.tistory.com/117
반응형
'Algorithm > Prob' 카테고리의 다른 글
Py) 프로그래머스 42861 섬 연결하기 (0) | 2022.02.11 |
---|---|
Py) 프로그래머스 43236 징검다리 (0) | 2022.02.10 |
Py) 프로그래머스 42885 구명보트 (0) | 2022.02.07 |
Py) 프로그래머스 43163 단어 변환 (0) | 2022.02.04 |
Py) 프로그래머스 42842 카펫 (0) | 2022.02.03 |
Comments