저고데

[프로그래머스][3차]방금그곡 본문

프로그래머스

[프로그래머스][3차]방금그곡

진철 2023. 2. 9. 21:23
728x90
반응형

문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/17683

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

코드를 작성하기 위한 순서는 다음과 같다.

1. 곡 재생시간을 반환하는 함수 transfer를 만들어준다.

2. 재생시간에 따라서 알맞게 멜로디를 늘려준다.

3. 멜로디에 #을 다르게 변환하는 함수 sharp를 만들어준다.

4. 멜로디에 m이 있으면 노래 제목과 재생시간을 담아준다.

5. 재생시간에 따라서 해당 배열을 내림차순 정렬해준다.

6. 배열의 가장 첫번째 원소에 해당하는 노래 제목을 반환한다.

def sharp(string): #3번 : m이 ABC일 때, ABCD와 ABC#은 문자열만 보았을 때, m이 포함된 것이다. 하지만 이는 다른 멜로디이므로 #을 따로 변환시켜줘야한다.
    string=string.replace('C#', 'c')
    string=string.replace('D#', 'd')
    string=string.replace('F#', 'f')
    string=string.replace('G#', 'g')
    string=string.replace('A#', 'a')
    return string

def transfer(first, second): #1번
    firsth=int(first.split(':')[0])
    firstm=int(first.split(':')[1])
    secondh=int(second.split(':')[0])
    secondm=int(second.split(':')[1])
    return (secondh*60+secondm)-(firsth*60+firstm)
    

def solution(m, musicinfos):
    answer = ''
    res=[]
    m=sharp(m)
    for i in range(len(musicinfos)):
        first, second, title, code=musicinfos[i].split(',')
        time=transfer(first, second)
        code=sharp(code)
        if len(code)<time: #2번
            code*=time//len(code)
            code+=code[0:time-len(code)]
        else:
            code=code[0:time]
        if m in code: #4번
            temp=[]
            temp.append(time)
            temp.append(title)
            res.append(temp)
    if not res:
        return "(None)"
    res.sort(key=lambda x:-x[0]) #5번
    answer+=res[0][1] #6번
    return answer

재생시간에 맞게 멜로디를 변환하는 것이 중요했던 것 같다.

그리고 C#과 C는 문자열로만 비교했을 때, 같다고 할 수 있지만 멜로디에서는 완전히 다르다는 것을 잠시동안 생각하지 못했다.

문제를 꼼꼼히 읽어보자. (제발 ㅠㅠ, 이거 때문에 답이 엉뚱하게 나와서 고생했어요 ㅠㅠ)

728x90
반응형