본문 바로가기
PROGRAMMERS

[프로그래머스/파이썬] [PCCP 기출문제] 1번 / 동영상 재생기

by kode-daks 2025. 1. 10.

내 풀이

def solution(video_len, pos, op_start, op_end, commands):
    answer = ''
    
    # 시간 str을 모두 초단위로 변환하여 계산하는 함수
    def time_to_sec(*time_string):
        sec_list = []
        for _t in time_string:
            mm, ss = map(int, _t.split(":"))
            ss += mm * 60
            sec_list.append(ss)
        return sec_list
    
	# 최종적으로 계산된 초를 시간 단위로 변환하는 함수
    def sec_to_time(second):
        mm = str(second // 60).rjust(2, '0')
        ss = str(second % 60).rjust(2, '0')
        return f"{mm}:{ss}"
    
    # 모든 시간 str을 초단위로 변환 후 사용
    video_len, pos, op_start, op_end = time_to_sec(video_len, pos, op_start, op_end)
    for command in commands:
        if op_start <= pos < op_end: # 이동 전후로 오프닝 생략
            pos = op_end
        if command == "next": # 가장 끝 위치를 넘어가지 않도록 조정
            pos = min(pos+10, video_len)  
        elif command == "prev": # 가장 첫 위치를 넘어가지 않도록 조정
            pos = max(pos-10, 0)
        if op_start <= pos < op_end: # 이동 전후로 오프닝 생략
            pos = op_end
            
    answer = sec_to_time(pos)
    return answer

 

메모

길이만큼 문자 채우기 : str.rjust(채울 개수, 채울 문자)

길이만큼 0 채우기 : str.zfill(채울 개수)