📖 문제
https://programmers.co.kr/learn/courses/30/lessons/17683
💻 코드
def parse(m):
p = list()
for i in range(len(m)):
if m[i] == '#':
continue
elif i != len(m) - 1 and m[i + 1] == '#':
p.append(m[i:i + 2] + '-')
else:
p.append(m[i] + '-')
return p
def solution(m, musicinfos):
# musicinfos 전처리
plays = list()
for info in musicinfos:
sl = info.split(',')
pl = parse(sl[3])
length = (int(sl[1].split(':')[0]) * 60 + int(sl[1].split(':')[1]))-(int(sl[0].split(':')[0]) * 60 + int(sl[0].split(':')[1]))
play = pl * (length // len(pl)) + pl[:(length % len(pl))]
plays.append((sl[2], ''.join(play), length))
# 방금그곡 후보 검색
candi = []
for play in plays:
if ''.join(parse(m)) in play[1]:
candi.append(play)
# 방금그곡 최종 선정
if len(candi) == 0:
return '(None)'
candi.sort(key=lambda x: x[2], reverse=True)
return candi[0][0]
🙌 한마디
구현(Implementation) 문제였고, in 연산자 사용 시 #이 붙은 음을 어떻게 구분하느냐가 핵심이었다. 한 음이 끝날 때 '-' 문자를 추가해 음이 종료되었음을 분별할 수 있도록 코드를 짰다. #이 붙은 음을 소문자로 바꾸어 구분짓는 다른 사람들의 풀이(ex.C#->c, F#->f)도 보았는데, 이것도 좋은 아이디어같다.
'Problem Solving > 📕Programmers' 카테고리의 다른 글
[Programmers] n진수 게임 (Python) (0) | 2021.10.08 |
---|---|
[Programmers] 캐시 (Python) (0) | 2021.10.07 |
[Programmers] 뉴스 클러스터링 (Python) (0) | 2021.10.06 |
[Programmers] 파일명 정렬 (Python) (0) | 2021.10.05 |
[Programmers] 프렌즈4블록 (Python) (0) | 2021.10.04 |