알고리즘/문제

[프로그래머스] Lv. 1 - [PCCP 기출문제] 1번 / 동영상 재생기

KoreaMango 2024. 11. 26. 23:42

제목: Lv. 1 - [PCCP 기출문제] 1번 / 동영상 재생기

시간: 30분

 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

PCCP 기출문제 1번 문제로 나온 동영상 재생기를 풀어봤습니다.

 

1번 문제답게 일반적으로 실무에서 사용될 만한 내용이었던  것 같아요.

 

문제를 딱 보자마자, 요구하는 기능들을 함수로 만들고 싶더라구요.

 

그래서 앞으로 넘기기, 뒤로 넘기기, 스킵하기, 문자열 정수로 만들기 이렇게 4개를 함수로 만들었어요.

 

import Foundation

func solution(_ video_len:String, _ pos:String, _ op_start:String, _ op_end:String, _ commands:[String]) -> String {
    var currentTime = minToSec(time: pos)
    let opStartTime = minToSec(time: op_start)
    let opEnd = minToSec(time: op_end)
    let videoLen = minToSec(time: video_len)

    for command in commands {
        currentTime = skipOp(currentTime: currentTime, start: opStartTime, end: opEnd)
        
        if (command == "next") {
            currentTime = nextTenSecond(video_len: videoLen, currentTime: currentTime)
        } 
        if (command == "prev") {
            currentTime = prevTenSecond(currentTime: currentTime)
        }
        
        currentTime = skipOp(currentTime: currentTime, start: opStartTime, end: opEnd)
    }
    
    return secToMin(time: currentTime)
}

func minToSec(time: String) -> Int {
    let times = time.split(separator: ":")
    let timeBySec = Int(times[0])! * 60 + Int(times[1])!
    
    return timeBySec
}

func secToMin(time: Int) -> String {
    let minInt = time / 60 
    let secInt = time % 60
    
    var min = minInt / 10 <= 0 ? "0\(minInt)" : "\(minInt)"
    var sec = secInt / 10 <= 0 ? "0\(secInt)" : "\(secInt)"
    
    return "\(min):\(sec)"
}

func nextTenSecond(video_len: Int, currentTime: Int) -> Int {
    var resultTime = currentTime
    resultTime += 10
    
    if (resultTime > video_len) {
        resultTime = video_len
    }
    
    return resultTime
}

func prevTenSecond(currentTime: Int) -> Int {
    var resultTime = currentTime
    resultTime -= 10
    
    if (resultTime < 0) {
        resultTime = 0
    }
      
    return resultTime
}

func skipOp(currentTime: Int, start: Int, end: Int) -> Int {
    if (start <= currentTime && end > currentTime) {
        return end
    }
    
    return currentTime
}

 

함수 하나에 하나의 기능만 들어가게 해서 실행했는데, 잘 통과되더라구요.

 

다른 분들 풀이도 보니까 저랑 생각이 비슷하신 것 같았어요.

 

사람 생각이 다 비슷한가...

 

거의 다 함수로 기능을 나눠서 풀이를 하셨더라구요.

 

이번 문제는 기능 구현하는 것 같아서 재미있었네요.