Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- 프로그래머스 파이썬
- 데이콘
- ubuntu
- Real or Not? NLP with Disaster Tweets
- 프로그래머스
- leetcode
- 더현대서울 맛집
- 캐치카페
- PYTHON
- Baekjoon
- Docker
- dacon
- SW Expert Academy
- 백준
- ChatGPT
- 우분투
- gs25
- Kaggle
- 금융문자분석경진대회
- 코로나19
- 맥북
- Git
- github
- 자연어처리
- hackerrank
- AI 경진대회
- 편스토랑 우승상품
- programmers
- 파이썬
- 편스토랑
Archives
- Today
- Total
솜씨좋은장씨
[BaekJoon] 15080번 : Every Second Counts (Python) 본문
728x90
반응형
코딩 1일 1문제! 오늘의 문제는 백준의 Every Second Counts 입니다.
🧑🏻💻 문제 풀이
24시간 규칙으로 되어있는 HH : MM : SS 형식의 문자열 2개를 입력 받으면
두 문자열 사이의 시간은 몇 초인지 구하는 문제 입니다.
시간 계산을 더 쉽게 하기 위해서
문자열을 datetime 의 strptime 함수를 활용하여 datetime 객체로 바꾼 뒤
두 객체의 차이를 구한 뒤 차이의 seconds 를 구하도록 했습니다.
% strptime / timedelta 참고 링크
2022.01.19 - [Programming/Python] - [Python] datetime의 strptime을 활용하여 시간과 시간 사이의 차이 구하는 방법
def convert_string_to_datetime(date_string):
return datetime.strptime(date_string, "%H : %M : %S")
먼저 strptime 에
10 : 0 : 0 의 형식인 %H : %M : %S 을 넣어 문자열을 datetime 으로 변환하는 함수를 만듭니다.
def calculate_elapsed_time(start_datetime, end_datetime):
elapsed_time = end_datetime - start_datetime
return elapsed_time.seconds
이렇게 변환한 datetime 객체의 차이를 구한 뒤
그 차이 값의 seconds 를 구해서 return 하는 함수를 만들어 줍니다.
이 두 함수를 활용하여 정답을 구하는 함수를 만들면 됩니다.
def every_second_counts(start_time, end_time):
start_datetime = convert_string_to_datetime(
date_string=start_time
)
end_datetime = convert_string_to_datetime(
date_string=end_time
)
elapsed_time_sec = calculate_elapsed_time(
start_datetime=start_datetime,
end_datetime=end_datetime
)
return elapsed_time_sec
전체 코드는 아래를 참고해주세요.
🧑🏻💻 코드 ( Solution )
from datetime import datetime
def convert_string_to_datetime(date_string):
return datetime.strptime(date_string, "%H : %M : %S")
def calculate_elapsed_time(start_datetime, end_datetime):
elapsed_time = end_datetime - start_datetime
return elapsed_time.seconds
def every_second_counts(start_time, end_time):
start_datetime = convert_string_to_datetime(
date_string=start_time
)
end_datetime = convert_string_to_datetime(
date_string=end_time
)
elapsed_time_sec = calculate_elapsed_time(
start_datetime=start_datetime,
end_datetime=end_datetime
)
return elapsed_time_sec
if __name__ == "__main__":
start_time = input()
end_time = input()
print(every_second_counts(start_time=start_time, end_time=end_time))
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaekJoon] 11536번 : 줄 세우기 (Python) (0) | 2022.11.06 |
---|---|
[BaekJoon] 23235번 : The Fastest Sorting Algorithm In The World (Python) (0) | 2022.11.05 |
[BaekJoon] 24263번 : 알고리즘 수업 - 알고리즘의 수행 시간 2 (Python) (0) | 2022.11.03 |
[BaekJoon] 18411번 : 試験 (Exam) (Python) (0) | 2022.11.02 |
[BaekJoon] 18409번 : 母音を数える (Counting Vowels) (Python) (0) | 2022.11.01 |
Comments