반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
** Python
1) 1차 시도 (실패 73.3/100)
from datetime import timedelta
def replace_melody(melody):
return melody\
.replace('C#', 'Z')\
.replace('D#', 'Y')\
.replace('F#', 'X')\
.replace('G#', 'W')\
.replace('A#', 'V')
def cal_time_diff(start, end):
start_hour, start_min = map(int, start.split(":"))
start_time = timedelta(hours=start_hour, minutes=start_min)
end_hour, end_min = map(int, end.split(":"))
end_time = timedelta(hours=end_hour, minutes=end_min)
return int((end_time - start_time).total_seconds() // 60)
def solution(m, musicinfos):
temp = [[0, 999, ""]]
for index, musicinfo in enumerate(musicinfos):
start_time, end_time, name, melody = musicinfo.split(",")
time_dur = cal_time_diff(start_time, end_time)
re_m = replace_melody(m)
re_melody = replace_melody(melody)
div, mod = divmod(time_dur, len(re_melody))
whole_music = re_melody * div + re_melody[0:mod] if len(re_melody) < time_dur else re_melody[0:mod]
if re_m in whole_music:
if temp[0][0] < time_dur:
temp[0] = [time_dur, index, name];
elif temp[0][0] == time_dur and temp[0][1] > index:
temp[0] = [time_dur, index, name];
if len(temp) > 0:
return temp[0][2]
return "(None)"
2) 2차 시도 (86.7/100)
from datetime import timedelta
def replace_melody(melody):
return melody\
.replace('C#', 'c')\
.replace('D#', 'd')\
.replace('E#', 'e')\
.replace('F#', 'f')\
.replace('G#', 'g')\
.replace('A#', 'a')\
.replace('B#', 'b')
def cal_time_diff(start, end):
start_hour, start_min = map(int, start.split(":"))
start_time = timedelta(hours=start_hour, minutes=start_min)
end_hour, end_min = map(int, end.split(":"))
end_time = timedelta(hours=end_hour, minutes=end_min)
return int((end_time - start_time).total_seconds() // 60)
def solution(m, musicinfos):
temp = [[0, 999, ""]]
for index, musicinfo in enumerate(musicinfos):
start_time, end_time, name, melody = musicinfo.split(",")
time_dur = cal_time_diff(start_time, end_time)
re_m = replace_melody(m)
re_melody = replace_melody(melody)
div, mod = divmod(time_dur, len(re_melody))
whole_music = re_melody * div + re_melody[0:mod] if len(re_melody) < time_dur else re_melody[0:mod]
if re_m in whole_music:
if time_dur > temp[0][0]:
temp[0] = [time_dur, index, name];
elif time_dur == temp[0][0]:
temp[0] = temp[0];
if temp[0][1] == 999:
return "(None)"
return temp[0][2]
3) 3차 시도 (결국 구글링 ㅠ)
def replace_melody(melody):
return melody\
.replace('C#', 'c')\
.replace('D#', 'd')\
.replace('F#', 'f')\
.replace('G#', 'g')\
.replace('A#', 'a')
def cal_time_diff(start, end):
start_hour, start_min = map(int, start.split(":"))
start_time = start_hour * 60 + start_min
end_hour, end_min = map(int, end.split(":"))
end_time = end_hour * 60 + end_min
return end_time - start_time
def solution(m, musicinfos):
answer = []
m = replace_melody(m)
for idx, musicinfo in enumerate(musicinfos):
musicinfo = replace_melody(musicinfo)
musicinfo = musicinfo.split(',')
time = cal_time_diff(musicinfo[0], musicinfo[1])
# 길이가 시간보다 더 긴 경우
if len(musicinfo[3]) >= time :
melody = musicinfo[3][0:time]
else:
# 시간을 계산해서 몫과 나머지로 계산
a, b = divmod(time, len(musicinfo[3]))
melody = musicinfo[3] * a + musicinfo[3][0:b]
# 노래가 melody에 포함되면 정답후보에 저장
if m in melody:
answer.append([idx, time, musicinfo[2]])
# 정답이 있는 경우
if len(answer) != 0:
# time -> index 기준으로 정렬
answer = sorted(answer, key = lambda x: (-x[1], x[0]))
return answer[0][2]
# 정답이 없는 경우
return "(None)"
참고: https://eda-ai-lab.tistory.com/506
조건문에서 잘못됐었던 것 같다
반응형
'프로그래머스 > level 2' 카테고리의 다른 글
[프로그래머스] n^2 배열 자르기 (0) | 2022.01.17 |
---|---|
[프로그래머스] 방문 길이 (0) | 2022.01.04 |
[프로그래머스] 파일명 정렬 (0) | 2021.12.28 |
[프로그래머스] 압축 (0) | 2021.12.28 |
[프로그래머스] 가장 큰 정사각형 찾기 (0) | 2021.12.27 |