From a27bbb31da2d861aa094bdb38c583dd8b3a5effc Mon Sep 17 00:00:00 2001 From: SOOOY <1401660@naver.com> Date: Mon, 7 Oct 2019 17:52:50 +0900 Subject: [PATCH] update hash problem --- ...73\355\225\234\354\204\240\354\210\230.py" | 9 ++++++ ...10\355\230\270\353\252\251\353\241\235.py" | 16 ++++++++++ ...354\213\23403_\354\234\204\354\236\245.py" | 16 ++++++++++ ...44\355\212\270\354\225\250\353\262\224.py" | 32 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 "SOOYEON/week1/\355\225\264\354\213\23401_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.py" create mode 100644 "SOOYEON/week1/\355\225\264\354\213\23402_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.py" create mode 100644 "SOOYEON/week1/\355\225\264\354\213\23403_\354\234\204\354\236\245.py" create mode 100644 "SOOYEON/week1/\355\225\264\354\213\23404_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" diff --git "a/SOOYEON/week1/\355\225\264\354\213\23401_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.py" "b/SOOYEON/week1/\355\225\264\354\213\23401_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.py" new file mode 100644 index 0000000..e69252f --- /dev/null +++ "b/SOOYEON/week1/\355\225\264\354\213\23401_\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.py" @@ -0,0 +1,9 @@ +# 풀이 1 - Counter 이용 +from collections import Counter + +def solution(participant, completion): + # Counter를 이용하면 리스트 내의 성분의 숫자를 딕셔너리 형태로 반환해준다 + # ex) list1 =['a', 'b', 'c', 'a'] => Counter(list1) : {'a':2, 'b':1, 'c':1} + not_completion = Counter(participant) - Counter(completion) # Counter의 경우 사칙연산도 가능 + + return list(not_completion.keys())[0] diff --git "a/SOOYEON/week1/\355\225\264\354\213\23402_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.py" "b/SOOYEON/week1/\355\225\264\354\213\23402_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.py" new file mode 100644 index 0000000..1a9d567 --- /dev/null +++ "b/SOOYEON/week1/\355\225\264\354\213\23402_\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.py" @@ -0,0 +1,16 @@ +# 시간복잡도 O(n^2) + +def solution(phone_book): + # phone_book 전체를 반복 => 기준 설정 + for phone_num in phone_book: + # 반복문을 통해 나머지 번호들과 비교 + for compare_num in phone_book: + if phone_num == compare_num: + continue + # 접두어 확인 + # ex) '119','1195524421' 비교 + # '1195524421'에서 '119'길이(3) 원소까지만 비교 + if phone_num in compare_num[:len(phone_num)]: + return False + + return True diff --git "a/SOOYEON/week1/\355\225\264\354\213\23403_\354\234\204\354\236\245.py" "b/SOOYEON/week1/\355\225\264\354\213\23403_\354\234\204\354\236\245.py" new file mode 100644 index 0000000..feaedcc --- /dev/null +++ "b/SOOYEON/week1/\355\225\264\354\213\23403_\354\234\204\354\236\245.py" @@ -0,0 +1,16 @@ +def solution(clothes): + # 1. 주어진 정보를 바탕으로 자료 구성 + clothes_info = {} # 의상 타입(key): 의상 수(value) + for c_name, c_type in clothes: + clothes_info[c_type] = clothes_info.get(c_type, 0) + 1 + + # 2. 경우의 수 구하기 + result = 1 # 조합 가능한 경우의 수 + # a1, a2, a3, b1, b2 의상이 존재할 때 경우의 수 + # (3 + 1) * (2 + 1) - 1 + # +1의 이유는 착용을 안하는 경우 + # 마지막 -1의 경우는 문제에서 제시된 최소 하나는 착용한다 + for value in clothes_info.values(): + result *= value + 1 + + return result - 1 diff --git "a/SOOYEON/week1/\355\225\264\354\213\23404_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" "b/SOOYEON/week1/\355\225\264\354\213\23404_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" new file mode 100644 index 0000000..4af5e6a --- /dev/null +++ "b/SOOYEON/week1/\355\225\264\354\213\23404_\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.py" @@ -0,0 +1,32 @@ +def solution(genres, plays): + GEN, PLAY = 0, 1 # 장르, 재생횟수 + VALUE = 1 # 딕셔너리에서 value의 인덱스 + + # 1. 주어진 정보 배열들을 알맞게 조작하기 + song_info = [(gen, play) for gen, play in zip(genres, plays)] + + dic_songs = {} # 장르(key): 노래정보(고유번호, 재생횟수) + dic_plays = {} # 장르(key): 재생횟수 합 + for idx, info in enumerate(song_info): + dic_songs[info[GEN]] = dic_songs.get(info[GEN], []) + [(idx, info[PLAY])] + dic_plays[info[GEN]] = dic_plays.get(info[GEN], 0) + info[PLAY] + + + # 2. 장르별 재생횟수 합이 높은 순서대로 정렬 + dic_plays = sorted(dic_plays.items(), key=lambda x: x[VALUE], reverse=True) + + + result = [] # 정답 배열 + + # 3. 2단계에서 구한 정보를 바탕으로 각 장르에서 재생횟수가 높은 2가지 음악 번호 배열에 담기 + for gen, plays in dic_plays: + best_songs = dic_songs.get(gen) # best_songs = [(고유번호, 재생횟수), ...] + best_songs = sorted(best_songs, key=lambda x: x[PLAY], reverse=True) + if len(best_songs) >= 2 : # 해당 장르의 곡이 2개 이상일 경우 + for idx in range(2): + result.append(best_songs[idx][0]) + else: # 2개보다 작으면 전부 다 넣어 + for idx in range(len(best_songs)): + result.append(best_songs[idx][0]) + + return result