[PCCP 기출문제] 1번 / 동영상 재생기

허상범·2025년 5월 4일
0

컴퓨팅 사고능력

목록 보기
1/1
post-thumbnail

머리가 안 돈다... 빡친다... 프로그래밍을 하도 안해서 그런지, 논리나 수치적으로 100% 이해하고 코딩을 하는게 아니라, 그냥 대충 직관으로 끄적이는 느낌이 많이 든다.
무슨 문제인지는 알겠는데, 이 얕은 느낌,,, lv1인데 ㅋㅋㅋ
1차원적인 논리만 생각할 줄 알고, 여러개 축으로 시나리오로 생각할 줄 모르는 이 느낌은 여전하구나.
논리를 먼저 쌓고 코드를 쓰는게 아니라, "아, 대충 이런 시나리오겠구나"하고 직관을 먼저 써서 코드를 쓰고, 논리는 사후적으로 잡는 느낌 같다.. 이러다보니 논리적으로 완결성이 안갖춰지면 상당히 찝찝하다
사고의 가지치기, 구조화가 구석구석, 디테일하게, 적극적으로 증명이 안되는 느낌이 싫다.

내 풀이

접근법

  • 일단, "mm:ss" 형태는 str 값이기 때문에, 숫자로 연산할 수 있게끔 형변환이 반드시 필요하다.
    - 한편, "mm:ss"라는 값을 초 데이터로 변환하더라도, 결과적으로 다시 "mm:ss"로 변환하는 함수도 독립적으로 필요하다.
    • 늘 그렇듯, 함수를 직접 만드는 것보다 자체 내장함수가 있는지, 아니면 pandas나 numpy 메서드가 있는지부터 찾아봤는데 없더라.
def solution(video_len, pos, op_start, op_end, commands):
    
    def time_to_sec(time):
        m, s = map(int, time.split(":"))
        sec = m*60+s
        return sec 

    def sec_to_time(sec):
        m = sec // 60
        s = sec % 60
        return f"{m}:{s}"

    result = time_to_sec("00:00")

    ### binding
    start = time_to_sec(op_start)
    end = time_to_sec(op_end)
    T = time_to_sec(video_len)
    t = time_to_sec(pos)
    
    
    for i in commands:
        # 10초 전 이동
        if i == "prev":
            if t < 10:
                result = time_to_sec("00:00")
            else:
                result = t-time_to_sec("00:10")
        # 10초 후 이동 
        elif i == "next":
            if T-t < time_to_sec("00:10"):
                return T
            else:
                result = t+time_to_sec("00:10")
                
        # 모든 명령을 실행했음에도 여전히 오프닝 구간이라면, opening skip 처리
        elif start <= t <= end:
            result = end

    result = sec_to_time(result)

    return result

타인의 풀이

가장 비슷한 접근법으로 풀이하신 분들을 참고해보면 아래와 같다

def time_to_seconds(time_str):
    minutes, seconds = map(int, time_str.split(':'))
    return minutes * 60 + seconds

def seconds_to_time(seconds):
    minutes, seconds = divmod(seconds, 60)
    return f"{minutes:02d}:{seconds:02d}"

def solution(video_len, pos, op_start, op_end, commands):
    video_len = time_to_seconds(video_len)
    current_pos = time_to_seconds(pos)
    op_start = time_to_seconds(op_start)
    op_end = time_to_seconds(op_end)

    for command in commands:
        if op_start <= current_pos <= op_end:
            current_pos = op_end

        if command == "prev":
            current_pos = max(0, current_pos - 10)
        elif command == "next":
            current_pos = min(video_len, current_pos + 10)

    # 마지막 명령 실행 후 한 번 더 오프닝 체크
    if op_start <= current_pos <= op_end:
        current_pos = op_end

    return seconds_to_time(current_pos)
    
    ```
profile
Engineering & Science, Since 2021 | Backend, Data, Economics

0개의 댓글