From a8cd1c13024aac63167ffb4b21d5a3687725ab15 Mon Sep 17 00:00:00 2001 From: sunyeongchoi Date: Fri, 22 Jan 2021 02:18:20 +0900 Subject: [PATCH] =?UTF-8?q?=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- argorithm/123add.py | 27 ++++++++++++++++++++++++ argorithm/123add_dp.py | 13 ++++++++++++ argorithm/best_album.py | 32 +++++++++-------------------- argorithm/best_album_others.py | 16 +++++++++++++++ argorithm/best_album_practice.py | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 22 deletions(-) create mode 100644 argorithm/123add.py create mode 100644 argorithm/123add_dp.py create mode 100644 argorithm/best_album_others.py diff --git a/argorithm/123add.py b/argorithm/123add.py new file mode 100644 index 0000000..6604c38 --- /dev/null +++ b/argorithm/123add.py @@ -0,0 +1,27 @@ +T = int(input()) + + +def dfs(n, number): + global cnt + if number == n: + cnt += 1 + return 0 + elif number > n: + return 0 + dfs(n, number+1) + dfs(n, number+2) + dfs(n, number+3) + + +for _ in range(T): + cnt = 0 + n = int(input()) + dfs(n, 0) + print(cnt) + + + + + + + diff --git a/argorithm/123add_dp.py b/argorithm/123add_dp.py new file mode 100644 index 0000000..8d1b670 --- /dev/null +++ b/argorithm/123add_dp.py @@ -0,0 +1,13 @@ +import sys + +T = int(sys.stdin.readline()) + +dp = [0, 1, 2, 4] + +for _ in range(T): + N = int(sys.stdin.readline()) + for i in range(len(dp), N+1): + print(i) + dp.append(dp[i-1] + dp[i-2] + dp[i-3]) + print(dp) + sys.stdout.write(str(dp[N]) + "\n") diff --git a/argorithm/best_album.py b/argorithm/best_album.py index 31e3992..66ea5f1 100644 --- a/argorithm/best_album.py +++ b/argorithm/best_album.py @@ -1,34 +1,22 @@ from collections import defaultdict -from operator import itemgetter def solution(genres, plays): + answer = [] + # 장르별 재생횟수 합계 구한 뒤 내림차순 정렬 genre_play_dict = defaultdict(lambda: 0) - print(genre_play_dict) for genre, play in zip(genres, plays): genre_play_dict[genre] += play - print(genre_play_dict) - - genre_rank = [genre for genre, play in sorted(genre_play_dict.items(), key=itemgetter(1), reverse=True)] - print(genre_rank) + sorted_genre_play_dict = sorted(genre_play_dict.items(), key=lambda x: x[1], reverse=True) - final_dict = defaultdict(lambda: []) - print(final_dict) - for i, genre_play_tuple in enumerate(zip(genres, plays)): - final_dict[genre_play_tuple[0]].append((genre_play_tuple[1], i)) - print(final_dict) - - answer = [] - for genre in genre_rank: - one_genre_list = sorted(final_dict[genre], key=itemgetter(0), reverse=True) - print(one_genre_list) - if len(one_genre_list) > 1: - answer.append(one_genre_list[0][1]) - answer.append(one_genre_list[1][1]) - else: - answer.append(one_genre_list[0][1]) + # 장르별 재생횟수 내림차순 + genre_play_list_dict = defaultdict(lambda: []) + for index, genre_play_tuple in enumerate(zip(genres, plays)): + genre_play_list_dict[genre_play_tuple[0]].append((index, genre_play_tuple[1])) - return answer + for genre in sorted_genre_play_dict: + answer += (sorted(genre_play_list_dict[genre[0]], key=lambda x: x[1], reverse=True)[:2]) + return [x[0] for x in answer] if __name__ == '__main__': diff --git a/argorithm/best_album_others.py b/argorithm/best_album_others.py new file mode 100644 index 0000000..9f75bdf --- /dev/null +++ b/argorithm/best_album_others.py @@ -0,0 +1,16 @@ +def solution(genres, plays): + answer = [] + d = {e: [] for e in set(genres)} + print(d) + for e in zip(genres, plays, range(len(plays))): + d[e[0]].append([e[1], e[2]]) + genreSort =sorted(list(d.keys()), key=lambda x: sum( map(lambda y: y[0], d[x])), reverse=True) + for g in genreSort: + temp = [e[1] for e in sorted(d[g], key=lambda x: (x[0], -x[1]), reverse=True)] + answer += temp[:min(len(temp), 2)] + return answer + + +if __name__ == '__main__': + real_answer = solution(['classic', 'pop', 'classic', 'classic', 'pop'], [500, 600, 150, 800, 2500]) + print(real_answer) diff --git a/argorithm/best_album_practice.py b/argorithm/best_album_practice.py index e69de29..36c069c 100644 --- a/argorithm/best_album_practice.py +++ b/argorithm/best_album_practice.py @@ -0,0 +1,35 @@ +from collections import defaultdict +from operator import itemgetter + + +def solution(genres, plays): + genre_play_dict = defaultdict(lambda: 0) + for genre, play in zip(genres, plays): + genre_play_dict[genre] += play + + genre_rank = [genre for genre, play in sorted(genre_play_dict.items(), key=itemgetter(1), reverse=True)] + print(genre_rank) + + final_dict = defaultdict(lambda: []) + print(final_dict) + for i, genre_play_tuple in enumerate(zip(genres, plays)): + print(genre_play_tuple) + final_dict[genre_play_tuple[0]].append((genre_play_tuple[1], i)) + print(final_dict) + + answer = [] + for genre in genre_rank: + one_genre_list = sorted(final_dict[genre], key=itemgetter(0), reverse=True) + print(one_genre_list) + if len(one_genre_list) > 1: + answer.append(one_genre_list[0][1]) + answer.append(one_genre_list[1][1]) + else: + answer.append(one_genre_list[0][1]) + + return answer + + +if __name__ == '__main__': + real_answer = solution(['classic', 'pop', 'classic', 'classic', 'pop'], [500, 600, 150, 800, 2500]) + print(real_answer)