Clock Conversion (Codeforces 1950C)
Tags: Math, implementation

Given the time in 24-hour format, output the equivalent time in 12-hour format.   24-hour format divides the day into 24 hours from $$$00$$$ to $$$23$$$, each of which has 60 minutes from $$$00$$$ to $$$59$$$.  12-hour format divides the day into two halves: the first half is $$$\mathrm{AM}$$$, and the second half is $$$\mathrm{PM}$$$. In each half, the hours are numbered in the order $$$12, 01, 02, 03, \dots, 11$$$. Each hour has 60 minutes numbered from $$$00$$$ to $$$59$$$. 

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1440$$$) — the number of test cases.The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from $$$00$$$ to $$$23$$$, and mm represents the minute from $$$00$$$ to $$$59$$$.The input will always be a valid time in 24-hour format.

Output: For each test case, output two strings separated by a space ("hh:mm AM" or "hh:mm PM"), which are the 12-hour equivalent to the time provided in the test case (without quotes).You should output the time exactly as indicated; in particular, you should not remove leading zeroes.


Sample Inputs:
11
09:41
18:06
12:14
00:59
00:00
14:34
01:01
19:07
11:59
12:00
21:37

Sample Outputs:
09:41 AM
06:06 PM
12:14 PM
12:59 AM
12:00 AM
02:34 PM
01:01 AM
07:07 PM
11:59 AM
12:00 PM
09:37 PM

Solution (Python):
t = int(input())
for i in range(t):
    l = input()
    l = l.split(':')
    if int(l[0]) < 12 and l[0] != '00':
        print(l[0] + ':' + l[1] + ' AM')
    elif l[0] != '00' and l[0] != '12' and (int(l[0]) - 12 < 10):
        print('0' + str(int(l[0]) - 12) + ':' + l[1] + ' PM')
    elif l[0] != '00' and l[0] != '12' and (int(l[0]) - 12 >= 10):
        print(str(int(l[0]) - 12) + ':' + l[1] + ' PM')
    elif l[0] == '00':
        print('12:' + l[1] + ' AM')
    elif l[0] == '12':
        print(l[0] + ':' + l[1] + ' PM')
    else:
        print('error')

Solution (Python):
for _ in range(int(input())):
    t = input().split(':')
    f = int(t[0])
    s = int(t[1])
    if f >= 13:
        f -= 12
        if len(str(f)) == 1:
            f = '0' + str(f)
        else:
            f = str(f)
        print(f + ':' + t[1] + ' PM')
    elif f == 12:
        print(t[0] + ':' + t[1] + ' PM')
    elif f == 0:
        print('12:' + t[1] + ' AM')
    else:
        print(t[0] + ':' + t[1] + ' AM')

Solution (Python):
for i in range(int(input())):
    s = input()
    wh = s[0] + s[1]
    p = ''
    if int(wh) < 12:
        if wh != '00':
            p = s + ' AM'
            print(p)
        else:
            wh = '12'
            p = wh + ':' + s[3] + s[4] + ' AM'
            print(p)
    else:
        found = True
        start = ''
        end = ''
        start = s[0] + s[1]
        end = s[3] + s[4]
        p = int(start)
        if p - 12 != 0:
            p = p - 12
        start = str(p)
        if p < 10:
            final = '0' + start + ':' + end + ' PM'
        else:
            final = start + ':' + end + ' PM'
        print(final)