Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SOOYEON/week1/해시01_완주하지못한선수.py
Original file line number Diff line number Diff line change
@@ -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의 경우 사칙연산도 가능

Comment on lines +7 to +8

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counter를 이용하는 방법이 굉장히 유용하군요 ! 💯

return list(not_completion.keys())[0]
16 changes: 16 additions & 0 deletions SOOYEON/week1/해시02_전화번호목록.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions SOOYEON/week1/해시03_위장.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions SOOYEON/week1/해시04_베스트앨범.py
Original file line number Diff line number Diff line change
@@ -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