728x90
https://school.programmers.co.kr/learn/courses/30/lessons/340213
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. Java
class Solution {
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
String answer = "";
String video_start = "00:00";
String pos_next = pos;
for ( String com : commands ) {
if ( op_start.compareTo(pos_next) <= 0 && op_end.compareTo(pos_next) > 0 ) {
pos_next = op_end;
}
String[] p = pos_next.split(":");
int p_next_mm = Integer.parseInt(p[0]);
int p_next_ss = Integer.parseInt(p[1]);
if ( com.equals("prev") ) {
p_next_ss -= 10;
if ( p_next_ss < 0 ) {
p_next_ss += 60;
p_next_mm--;
}
} else if ( com.equals("next") ) {
p_next_ss += 10;
if ( p_next_ss >= 60 ) {
p_next_ss -= 60;
p_next_mm++;
}
}
// pos_next = String.format("%02d", p_next_mm)+":"+String.format("%02d", p_next_ss);
pos_next = (p_next_mm < 10 ? "0"+p_next_mm : p_next_mm) + ":" + (p_next_ss < 10 ? "0"+p_next_ss : p_next_ss);
if ( op_start.compareTo(pos_next) <= 0 && op_end.compareTo(pos_next) > 0 ) {
pos_next = op_end;
} else if ( pos_next.compareTo(video_start) < 0 ) {
pos_next = video_start;
} else if (pos_next.compareTo(video_len) > 0 ) {
pos_next = video_len;
}
}
answer = pos_next;
return answer;
}
}
2. Python
def solution(video_len, pos, op_start, op_end, commands):
answer = ''
video_start = "00:00"
pos_next = pos
for com in commands :
if op_start <= pos_next and op_end > pos_next :
pos_next = op_end
p = pos_next.split(":")
p_next_mm = int(p[0])
p_next_ss = int(p[1])
if com == "prev" :
p_next_ss -= 10
if p_next_ss < 0 :
p_next_ss += 60
p_next_mm -= 1
elif com == "next" :
p_next_ss += 10
if p_next_ss >= 60 :
p_next_ss -= 60
p_next_mm += 1
# pos_next = "{:>02}".format(p_next_mm) + ":" + "{:>02}".format(p_next_ss)
# pos_next = (f"{p_next_mm:>02}") + ":" + (f"{p_next_ss:>02}")
# pos_next = ("%02d" % p_next_mm) + ":" + ("%02d" % p_next_ss)
# pos_next = format(p_next_mm, "02d") + ":" + format(p_next_ss, "02d")
pos_next = (str(0) + str(p_next_mm) if p_next_mm < 10 else str(p_next_mm)) + ":" + (str(0) + str(p_next_ss) if p_next_ss < 10 else str(p_next_ss))
if op_start <= pos_next and op_end > pos_next :
pos_next = op_end
elif pos_next < video_start :
pos_next = video_start
elif pos_next > video_len :
pos_next = video_len
answer = pos_next
return answer
※ Python string formatter (https://docs.python.org/3/library/string.html#string.Formatter)
반응형