https://school.programmers.co.kr/learn/courses/30/lessons/17683

 

프로그래머스

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

programmers.co.kr

문제 풀이

  1. 문자열 시간을 계산하는 방법 : datetime.strptime(문자열 시간, 형식) / 시간차이.seconds (days는 없다.)
  2. #을 처리하기 위해 C# → c 으로 변경했다. 이를 위해서 배열을 새로만들었다… 더 좋은 방법이 있을 것 같지만…
  3. change 코드 수정!
  4. 시간 계산은 굳이 datetime함수를 사용하지 않아도 간단히 계산할 수 있다. (end_h - start_h) * 60 + (end_m - start_m) datetime을 하면 실행시간이 너무 길다!

코드

def change(code):
    code=list(code)
    sharp=['C','D','F','G','A']
    new=[]
    for i in range(len(code)):
        if i==len(code)-1:
            if code[i] != '#':
                new.append(code[i])
                break
        if code[i] in sharp and code[i+1]=='#':
            new.append(code[i].lower())
        elif code[i] !='#':
            new.append(code[i])
    code=''.join(new)
    return code

def solution(m, musicinfos):
    answer = '(None)'
    for music in musicinfos:
        start,end,title,code=music.split(',')
        end_h, end_m = map(int, end.split(':'))
        start_h, start_m = map(int, start.split(':')) 
        time = (end_h - start_h) * 60 + (end_m - start_m)
        
        #start=datetime.datetime.strptime(start,"%H:%M")
        #end=datetime.datetime.strptime(end,"%H:%M")
        #time=(end-start).seconds//60 #재생시간
        
        code=change(code)
        m=change(m)
        play_code=''
        i=0
        temp_time=time
        while temp_time>0:
            if i>len(code)-1:
                i=0
            play_code+=code[i]
            i+=1
            temp_time-=1
        if m in play_code:
            if answer=='(None)':
                answer=title
                answer_time=time
            else:
                if time>answer_time:
                    answer=title
                    answer_time=time
    return answer