https://programmers.co.kr/learn/courses/30/lessons/17683
코딩테스트 연습 - [3차] 방금그곡
방금그곡 라디오를 자주 듣는 네오는 라디오에서 방금 나왔던 음악이 무슨 음악인지 궁금해질 때가 많다. 그럴 때 네오는 다음 포털의 '방금그곡' 서비스를 이용하곤 한다. 방금그곡에서는 TV,
programmers.co.kr
✅ Solution
- replace를 이용해서 musicinfos에서 # 붙은 음들을 다른 알파벳으로 바꿔준다.
- 재생 시간을 계산한 뒤, 해당 재생시간 동안의 음악을 music 변수에 할당한다.
- 만약 m 이 music 내 문자열에 포함되어 있다면, answer에 재생 시간과 곡 명을 넣어준다.
- time이 최대인 곡 명을 찾아 반환한다.
✅ Code
def solution(m, musicinfos):
answer = []
m = m.replace('A#', 'H').replace('C#', 'I').replace('D#', 'J').replace('F#', 'K').replace('G#', 'L')
for musicinfo in musicinfos:
musicinfo = musicinfo.split(',')
musicinfo[3] = musicinfo[3].replace('A#', 'H').replace('C#', 'I').replace('D#', 'J').replace('F#', 'K').replace(
'G#', 'L')
time = (int(musicinfo[1].split(':')[0]) * 60 + int(musicinfo[1].split(':')[1])) - (
int(musicinfo[0].split(':')[0]) * 60 + int(musicinfo[0].split(':')[1]))
music = ''.join(musicinfo[3] * (time // len(musicinfo[3])) + musicinfo[3][:time % len(musicinfo[3])])
if m in music:
answer.append((time, musicinfo[2]))
if len(answer) > 0:
answer.sort(key=lambda x: -x[0])
return answer[0][1]
return "(None)"
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 양궁대회 (Python) (2) | 2022.05.24 |
---|---|
[프로그래머스] Level 2 - [3차] 압축 (Python) (0) | 2022.05.20 |
[프로그래머스] Level 2 - [1차] 캐시 (Python) (0) | 2022.05.17 |
[프로그래머스] Level 2 - [1차] 프렌즈4블록 (Python) (0) | 2022.05.17 |
[프로그래머스] Level 2 - 튜플 (Python) (0) | 2022.05.13 |