[Solved] Code Coach - Military Time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

[Solved] Code Coach - Military Time

Hello everyone! I tried the challenge “Military Time” many times but there was always two hidden test cases that were wrong... Could please someone propose some python code that would work?

15th Apr 2021, 6:25 AM
Maisu
Maisu - avatar
7 Answers
+ 2
To solve all the tests, these codes are enough, given that the input is always of the same form: a, b = input().split(":") if b[-2] == 'P': a = str((int(a)+12)%24) print(f"{a.zfill(2)}:{b[:2]}") # Python Version (not very pythonic) #include <stdio.h> int main() { int a; char b[3], c; scanf("%d:%s %c", &a, b, &c); if (c == 80) a = (a + 12) % 24; if (a < 10) putchar(48); printf("%d:%s", a, b); return 0; } // C version Hope this helps. P.S. The problem your code had was that it didn't put a leading zero for an hour-length of 1.
15th Apr 2021, 8:01 AM
Calvin Thomas
Calvin Thomas - avatar
+ 4
Here one of my trials for “No Numerals”: phrase = input().split() output = "" for i in phrase: if i == "0": i = "zero" if i == "1": i = "one" if i == "2": i = "two" if i == "3": i = "three" if i == "4": i = "four" if i == "5": i = "five" if i == "6": i = "six" if i == "7": i = "seven" if i == "8": i = "eight" if i == "9": i = "nine" if i == "10": i = "zero" output += i + " " print(output) I am still learning and I know my code is really bad but please help me to improve myself.
15th Apr 2021, 6:29 AM
Maisu
Maisu - avatar
+ 3
phrase = input().split() output = "" for i in phrase: if i == "0": i = "zero" if i == "1": i = "one" if i == "2": i = "two" if i == "3": i = "three" if i == "4": i = "four" if i == "5": i = "five" if i == "6": i = "six" if i == "7": i = "seven" if i == "8": i = "eight" if i == "9": i = "nine" if i == "10": i = "ten" output += i + " " print(output)
15th Apr 2021, 6:35 AM
Atul [Inactive]
+ 2
Here is on of my trials for “Military Time”: input = input().split() time = input[0] post = input[1] if post == "AM": print(time) else: splited = time.split(":") hours = int(splited[0]) minutes = splited[1] hours += 12 print(f"{hours}:{minutes}")
15th Apr 2021, 6:33 AM
Maisu
Maisu - avatar
+ 2
Make ten instead of 0
15th Apr 2021, 6:35 AM
Atul [Inactive]
+ 2
Oh... thank you! It worked, I am so stupid...
15th Apr 2021, 6:36 AM
Maisu
Maisu - avatar
+ 1
Maisu Regarding your request for a pythonic approach to Military Time. Please consider the use of %12 to ascertain the hours depending on AM or PM. Also, many people struggle with 12:01 AM, which requires 00:01 as the output. So you will need to be able to identify if your output has 1 or 2 digits in the hour section. If your result is 0:01 for 12:01 AM, then you will need to adjust for that. A few thoughts for you that may help
15th Apr 2021, 10:06 AM
Rik Wittkopp
Rik Wittkopp - avatar