Time Conversion

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

 

Example

  • s = '12:01:00PM'
  • Return '12:01:00'.
  • s = '12:01:00AM'
  • Return '00:01:00'.

 

Function Description

Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

  • string s: a time in 12 hour format

 

Returns

  • string: the time in 24 hour format

 

Input Format

A single string s that represents a time in 12-hour clock format (i.e.:  hh:mm:ssAMor hh:mm:ssPM ).

 

Constraints

  • All input times are valid

 

Sample Input

07:05:45PM

 

Sample Output

19:05:45

 


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'timeConversion' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#

def timeConversion(s):
    # Write your code here
    if s.endswith('PM'):
        hh, mm, ss = s.split(':')
        if int(hh) < 12:
            new_hh = str(int(hh)+12)
            s = new_hh + ':' + mm + ":" + ss[:-2]
            print(s)
            return s
    else:
        hh, mm, ss = s.split(':')
        new_hh = str(int(hh)-12).zfill(2)
        s = new_hh + ':' + mm + ":" + ss[:-2]
        print(s)
        return s

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = timeConversion(s)

    fptr.write(result + '\n')

    fptr.close()

 

 

테스트 케이스 2, 4, 6 실패

헉. Test case 2, 4, 6번을 통과하지 못 했다.  

 

해커랭크는 Test case를 공개해준다! 매우 땡베감 .. (5개 한정인듯)

 

 

테스트 케이스를 분석해보니, 올바르게 AM/PM을 잘 입력했을 때 리턴 값을 정해진 출력 형식에 맞추지 않아서 통과하지 못했다. 

따라서 아래와 같이 코드를 수정해주었다.

 

def timeConversion(s):
    # Write your code here
    if s.endswith('PM'):
        hh, mm, ss = s.split(':')
        if int(hh) < 12:
            new_hh = str(int(hh)+12)
            s = new_hh + ':' + mm + ":" + ss[:-2]
            return s
        else:
            print(s[:-2])
            return s[:-2]
    else:
        hh, mm, ss = s.split(':')
        if int(hh) < 12:
            print(s[:-2])
            return s[:-2]
        else:
            new_hh = str(int(hh)-12).zfill(2)
            s = new_hh + ':' + mm + ":" + ss[:-2]
            print(s)
            return s

 

통과가 되었다! 굿!


새로 알게 된 함수

string.endswitch(value) : 문자열이 value로 끝나는 문자열

string.startswitch(value) : 문자열이 value로 시작하는 문자열

 

str(str_num).zfill(num) : 한 자릿 수 숫자를 원하는 여러 자릿수 형태로 만들어주는 함수 

ex) str(7).zfill(2) | output : `07` 

 


코딩테스트를 오랜만에 하다보니 머리가 굳었다. 하지만 여전히 재밌다!

'알고리즘 > HackerRank' 카테고리의 다른 글

[HackerRank] Diagonal Difference  (0) 2023.06.13
[HackerRank] Lonely Integer  (0) 2023.06.13
[HackerRank] Mini-Max Sum  (0) 2023.06.12
[HackerRank] Plus Minus  (0) 2023.06.12
[HackerRank] 해커랭크 코딩테스트 준비  (0) 2023.06.12

+ Recent posts