import datetime

def solution(m, musicinfos):
    result = ('(None)', 0)

    for musicinfo in musicinfos:
        musicinfo = musicinfo.split(',')
        start, start_hour, start_min = musicinfo[0], int(musicinfo[0][:2]), int(musicinfo[0][3:5])
        end, end_hour, end_min = musicinfo[1], int(musicinfo[1][:2]), int(musicinfo[1][3:5])
        title = musicinfo[2]
        splited_content = split_content(musicinfo[3])

        d = datetime.datetime(2018, 5, 19, end_hour, end_min) - datetime.datetime(2018, 5, 19, start_hour, start_min)
        minutes = d.seconds // 60

        splited_all_content = splited_content*(minutes//len(splited_content)) + splited_content[ :minutes%len(splited_content)]

        if is_contain(split_content(m), splited_all_content):
            if result[0]=='(None)' or result[1]<minutes:
                result = (title, minutes)

    return result[0]


def split_content(content):
    content = list(content)
    while '#' in content:
        for i in range(len(content)-1):
            if content[i+1]=='#':
                content[i:i+2] = [''.join(content[i:i+2])]
                break

    return content


def is_contain(lst1, lst2):
    if len(lst1) > len(lst2):
        return False

    for i in range(len(lst2)):
        for j in range(len(lst1)):
            if i+len(lst1) > len(lst2):
                break
            if lst2[i+j]!=lst1[j]:
                break
        else:
            return True

    return False

생각해보기

  • C와 C#을 구분하여 잘라서 새로운 리스트를 만들어 주는 split_content() 함수를 구현하였다.

  • 다른 방법으로는 C#, D#, E#, ...을 c, d, e, ...로 변환하는 방법도 있다.

+ 따끈한 최근 게시물