def repl(m) : m = m.replace("A#", 'a') m = m.replace("C#", 'c') m = m.replace("D#", 'd') m = m.replace("F#", 'f') m = m.replace("G#", 'g') return m def time_code(start, end) : s_h, s_m = start.split(":") e_h, e_m = end.split(":") time = (int(e_h)*60 + int(e_m)) - (int(s_h)*60 + int(s_m)) return time def solution(m, musicinfos): answer = [] m = repl(m) for musicinfo in musicinfos : start, end, title, music = musicinfo.split(',') time = time_code(start, end) music = repl(music) if time > len(music) : q, r = divmod(time, len(music)) music = music * q + music[:r] else : music = music[:time] if m in music : answer.append([time, title]) if len(answer) != 0: answer.sort(key=lambda x : x[0], reverse=True) return answer[0][1] else : return "(None)"
풀이
1) # 처리 해주는 함수
def repl(m) : m = m.replace("A#", 'a') m = m.replace("C#", 'c') m = m.replace("D#", 'd') m = m.replace("F#", 'f') m = m.replace("G#", 'g') return m
replace 함수를 통해 '0#' 을 모두 소문자로 변환해주었다.
2) 시간 계산 함수
def time_code(start, end) : s_h, s_m = start.split(":") e_h, e_m = end.split(":") time = (int(e_h)*60 + int(e_m)) - (int(s_h)*60 + int(s_m)) return time
시
와 분
으로 나눈후 끝나는 시간 - 시작한 시간을 계산해 플레이시간을 구한다.
3) 메인 함수 - 멜로디 늘리고 줄이기
def solution(m, musicinfos): answer = [] m = repl(m) for musicinfo in musicinfos : start, end, title, music = musicinfo.split(',') time = time_code(start, end) music = repl(music) if time > len(music) : q, r = divmod(time, len(music)) music = music * q + music[:r] else : music = music[:time] if m in music : answer.append([time, title])
1. 시작시간
, 끝나는 시간
, 제목
, 음악
을 split함수로 나누어준다.
2. 위(2))의 시간 함수를 통해 플레이 시간
을 구한다.
3. 위(1))의 # 처리 함수를 통해 #을 처리해준다.(m도 마찬가지)
4. 만약 플레이 시간
이 음악
보다 길다면 음악
을 플레이 시간
만큼 늘려준다.
- divmod 함수 : 입력된 두 값을 이용하여 몫과 나머지를 구한다.
5. 그렇지 않다면 음악
을 줄여준다.
6. 그리고 m
이 변환한 음악
안에 있다면 [플레이 시간
, 제목
]을 answer
에 추가한다.
4) 메인함수 - 정답 return 하기
if len(answer) != 0: answer.sort(key=lambda x : x[0], reverse=True) return answer[0][1] else : return "(None)"
1. 만약 answer
에 값이 있다면 플레이 시간
을 기준으로 정렬해준뒤, 맨 처음의 값의 제목
을 반환해준다.
2. answer
에 값이 없다는 것은 일치하는 음악이 없다는 뜻이므로 "(None)"
을 반환한다.
'Algorithm > Programmers' 카테고리의 다른 글
[Python] 소수 찾기 (0) | 2021.06.18 |
---|---|
[Python] 압축 (0) | 2021.06.15 |
[Python] 캐시 (0) | 2021.06.13 |
[Python] 이진 변환 반복하기 (0) | 2021.06.13 |
[Python] 프렌즈4블록 (0) | 2021.06.11 |