[프로그래머스] PCCP 기출 1 | 동영상 재생기

Gaanii·2024년 10월 10일
0

Problem Solving

목록 보기
17/210
post-thumbnail

아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀



풀이과정


간단하게 생각하면 그냥 냅다 초 단위로 싹 바꿔버리면 되는 문제다.

하지만 오랜만에 문제푸니 그런 생각 조차 안들고 그냥 노가다 뛴 나자신 ,,,

그래도 조건은 잘 맞춰서 풀었다(가 아니라 사실 테스트 케이스 6번만 못뚫어서 노가다로 모든 조건 맞춤)

코드


def checkOpening(start_m, start_s, end_m, end_s, pos_m, pos_s):
    if ((start_m < pos_m < end_m) or \
        (start_m < pos_m == end_m and pos_s <= end_s) or \
        (start_m == pos_m < end_m and start_s <= pos_s) or \
        (start_m == pos_m == end_m and start_s <= pos_s <= end_s)):
            return end_m, end_s
    else:
        return pos_m, pos_s

def solution(video_len, pos, op_start, op_end, commands):
    video_len_m, video_len_s = map(int, video_len.split(":"))
    pos_m, pos_s = map(int, pos.split(":"))
    start_m, start_s = map(int, op_start.split(":"))
    end_m, end_s = map(int, op_end.split(":"))

    pos_m, pos_s = checkOpening(start_m, start_s, end_m, end_s, pos_m, pos_s)

    for comm in commands:
        # comm이 next인 경우 10초를 더했을 때
        # 1. 비디오 길이를 벗어나는지 체크
        # 2. 비디오 길이 내에 있을때 초가 60초 이상인지 체크
        # 2-1. 이 때 비디오 길이를 넘었을 때도 체크
        # 3. 위 조건에 걸리지 않는 모든 경우는 초만 10초 증가
        if comm == 'next':
            if pos_m == video_len_m and pos_s + 10 >= video_len_s:
                pos_m, pos_s = video_len_m, video_len_s
            elif pos_m < video_len_m and pos_s + 10 >= 60:
                pos_m, pos_s = pos_m + 1, (pos_s + 10) % 60
                if pos_m >= video_len_m and pos_s >= video_len_s:
                    pos_m, pos_s = video_len_m, video_len_s
            else:
                pos_s += 10


        # comm이 prev인 경우 10초를 뺐을 때
        # 1. 비디오 길이를 벗어나는지 체크
        # 2. 비디오 길이 내에 있을때 초가 0초 미만인지 체크
        # 13:00 10 12 50 13:07 10 12:57
        # 3. 위 조건에 걸리지 않는 모든 경우는 초만 10초 감소
        else:
            if pos_m == 0 and pos_s - 10 <= 0:
                pos_m = pos_s = 0
            elif pos_m > 0 and pos_s - 10 < 0:
                pos_m, pos_s = pos_m - 1, 50 + pos_s
            else:
                pos_s -= 10

        pos_m, pos_s = checkOpening(start_m, start_s, end_m, end_s, pos_m, pos_s)

    if pos_m // 10 == 0:
        pos_m = '0' + str(pos_m)
    if pos_s // 10 == 0:
        pos_s = '0' + str(pos_s)

    answer = str(pos_m) + ':' + str(pos_s)
    return answer


결과


정답

0개의 댓글