Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Docker
- 편스토랑
- Real or Not? NLP with Disaster Tweets
- 프로그래머스
- 데이콘
- Baekjoon
- PYTHON
- 우분투
- ubuntu
- ChatGPT
- AI 경진대회
- dacon
- 자연어처리
- 캐치카페
- 맥북
- 금융문자분석경진대회
- 편스토랑 우승상품
- gs25
- Kaggle
- github
- hackerrank
- 백준
- leetcode
- 코로나19
- 프로그래머스 파이썬
- 파이썬
- programmers
- Git
- SW Expert Academy
- 더현대서울 맛집
Archives
- Today
- Total
솜씨좋은장씨
[Programmers] 해시 : 베스트앨범 (Python) 본문
728x90
반응형
1일 1문제 21일차!
오늘의 문제는 프로그래머스 베스트 앨범 입니다.
첫번째 시도
dictionary를 활용해보기로 했습니다.
def solution(genres, plays):
answer = []
if len(genres) > 1:
genres_dic = {genres[0]:[(plays[0], 0)]}
total_plays_dic = {genres[0]:plays[0]}
for i in range(1, len(genres)):
if genres[i] not in genres_dic.keys():
genres_dic[genres[i]] = [(plays[i], i)]
total_plays_dic[genres[i]] = plays[i]
else:
genres_dic[genres[i]].append((plays[i], i))
total_plays_dic[genres[i]] = total_plays_dic[genres[i]] + plays[i]
sorted_play_dic = sorted(total_plays_dic, reverse=True)
for key in sorted_play_dic:
play_time_list = genres_dic[key]
play_time_list = sorted(play_time_list, key=lambda x:(-x[0], x[1]))
if len(play_time_list) >= 2:
for i in range(2):
answer.append(play_time_list[i][1])
else:
for i in range(len(play_time_list)):
answer.append(play_time_list[i][1])
else:
answer.append(0)
return answer
두번째 시도
def solution(genres, plays):
answer = []
genres_dic = {}
total_plays_dic = {}
if len(genres) == 1:
return [0]
else:
for i in range(len(genres)):
if genres[i] not in genres_dic.keys():
genres_dic[genres[i]] = [(plays[i], i)]
total_plays_dic[genres[i]] = plays[i]
else:
genres_dic[genres[i]].append((plays[i], i))
total_plays_dic[genres[i]] = total_plays_dic[genres[i]] + plays[i]
sorted_play_dic = sorted(total_plays_dic, reverse=True)
for key in sorted_play_dic:
play_time_list = genres_dic[key]
if len(play_time_list) >= 2:
play_time_list = sorted(play_time_list, key=lambda x:(-x[0], x[1]))
for i in range(2):
answer.append(play_time_list[i][1])
else:
for i in range(len(play_time_list)):
answer.append(play_time_list[i][1])
return answer
세번째 시도
def solution(genres, plays):
answer = []
genres_dic = {}
total_plays_dic = {}
if len(genres) == 1:
return [0]
else:
for i in range(len(genres)):
if genres[i] not in genres_dic.keys():
genres_dic[genres[i]] = [(plays[i], i)]
total_plays_dic[genres[i]] = plays[i]
else:
genres_dic[genres[i]].append((plays[i], i))
total_plays_dic[genres[i]] = total_plays_dic[genres[i]] + plays[i]
sorted_play_dic = sorted(total_plays_dic, reverse=True)
for key in sorted_play_dic:
play_time_list = genres_dic[key]
play_time_list = sorted(play_time_list, key=lambda x:(-x[0], x[1]))
if len(play_time_list) >= 2:
for i in range(2):
answer.append(play_time_list[i][1])
else:
answer.append(play_time_list[i][1])
return answer
네번째 시도
def solution(genres, plays):
answer = []
genre_total_play = {}
genre_dic = {}
for i in range(len(genres)):
if genres[i] not in genre_dic.keys():
genre_dic[genres[i]] = [(plays[i], i)]
genre_total_play[genres[i]] = plays[i]
else:
genre_dic[genres[i]].append((plays[i], i))
genre_total_play[genres[i]] = genre_total_play[genres[i]] + plays[i]
# print(genre_total_play)
# print(genre_dic)
sorted_total_play = sorted(genre_total_play.items(), key=lambda x: x[1], reverse=True)
print(sorted_total_play)
for key in sorted_total_play:
play_list = genre_dic[key[0]]
play_list = sorted(play_list, key = lambda x : (-x[0], x[1]))
for i in range(len(play_list)):
if i == 2:
break
answer.append(play_list[i][1])
return answer
문제는 정렬시에 items를 붙이지 않고 정렬했던 것이 문제였습니다.
두 번째 런타임 오류때 파악을 했어야했는데 그러지 못한게 아쉬웠던 문제입니다.
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 7. Median of Two Sorted Arrays (Python) (2) | 2020.02.29 |
---|---|
[Programmers] 완전탐색 : 소수 찾기 (Python) (0) | 2020.02.28 |
[Programmers] 스택/큐 : 주식가격 (Python) (0) | 2020.02.26 |
[Programmers] 스택/큐 : 기능개발 (Python) (0) | 2020.02.25 |
[Programmers] 해시 : 전화번호 목록 (Python) (9) | 2020.02.24 |
Comments