+ 12

Military Time Code Coach Problem

Alright, guys, please run this peace of code if you would - doesn't it show perfect results for military time conversion as specified in the task? Any idea how my solution could lead to wrong test results (which it does)? def f(inp): hours = int(inp[:2]) minutes = inp[3:5] if 'PM' in inp: print(f'{(hours+12)%24:02d}:{minutes}') else: print(f'{hours:02d}:{minutes}') for i, t in zip(range(24), 12*['AM']+12*['PM']): for j in range(0, 60, 5): f(f'{i%12:02d}:{j:02d} {t}')

20th Dec 2019, 1:05 AM
HonFu
HonFu - avatar
27 Réponses
+ 14
Does your method also work for this input format: X:XX Edit: No f("1:12 PM") don't work f("01:12 PM") works correct
20th Dec 2019, 1:34 AM
Denise Roßberg
Denise Roßberg - avatar
+ 11
Denise Roßberg, you're right, that was the issue! Funny, I kept looking at the output side, when really the input section caused the trouble. After adding this, it worked fine: if len(inp)==7: inp = '0'+inp EDIT: And now, after I remembered that this is python, I took these two lines right out and did: inp = input().zfill(8)
20th Dec 2019, 8:29 AM
HonFu
HonFu - avatar
+ 5
I had exactly the same issue. Like Denise Roßberg said, it comes from the output format, which must be XX:XX. Simply add a "0" at the beginning if necessary.
20th Dec 2019, 6:36 AM
Théophile
Théophile - avatar
+ 5
Oh, I'm thinking too simple. I've split() the input two times. First at the blank, and then the left part at the colon. So I don't ran into this issue 😮
20th Dec 2019, 3:23 PM
Coding Cat
Coding Cat - avatar
+ 5
So my lazyness got me into trouble. 😁
20th Dec 2019, 3:26 PM
HonFu
HonFu - avatar
+ 4
I see, let me test them one by one tomorrow 😋
12th Feb 2020, 5:09 PM
Gordon
Gordon - avatar
+ 3
Guys, why are you all posting your specific solutions here? This is not what the question was about! Think of the other sololearners too, we shouldn't for no reason leave boxes with solutions standing around!
16th Jan 2020, 11:52 AM
HonFu
HonFu - avatar
+ 3
Try this python code: sentence = input() list = sentence.split(':') if 'PM' in sentence: sentence = str(int(list[0])+12)+':'+list[1] if 'AM' in sentence and int(list[0])<=9: sentence = '0'+list[0]+':'+list[1] list = sentence.split(' ') print(list[0])
5th Feb 2020, 8:42 AM
RAHUL SALIYA
RAHUL SALIYA - avatar
+ 3
Sultan 02d in {:02d} is string formatting.
12th Feb 2020, 5:00 PM
Gordon
Gordon - avatar
+ 3
It takes a string and fills it up with zeros at the beginning until it has the given length. (Well, returns a new string, immutable blabla you know.)
12th Feb 2020, 5:00 PM
HonFu
HonFu - avatar
+ 3
HonFu Oh I see, kindof opposite to left-truncate, interesting ~
12th Feb 2020, 5:05 PM
Gordon
Gordon - avatar
+ 2
Théophile the code seem to do that already
20th Dec 2019, 6:48 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
В чём проблема? Почему данный код не работоспособен? На ПК он нормально проходит любые тесты. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string[] input=Console.ReadLine().ToLower().Split(' '); // input[0]="10:00"; if(input[1]=="am") {clock(ref input,true);} else if (input[1]=="pm") {clock(ref input,false);} //Console.WriteLine("24:"+input[1]); Console.WriteLine(string.Format("{0:00}:{1:00}",input[0],input[1])); } static void clock(ref string[] input,bool am) { input=input[0].Split(':'); int hour=Convert.ToInt32(input[0]); hour=hour==12?0:hour; if(!am) {hour +=12;} input[0]=hour.ToString(); } } }
16th Jan 2020, 11:48 AM
Александр
+ 2
what's zfill()?
12th Feb 2020, 4:58 PM
Gordon
Gordon - avatar
+ 2
Yeah, there are a few other nice ones: rjust, ljust and center, where you can stretch a string to a certain width and place the text in there. There are so many string methods...
12th Feb 2020, 5:08 PM
HonFu
HonFu - avatar
+ 1
t=input().strip() numt,ch=t.split(' ') hour,minute=numt.split(':') hour=int(hour) if ch=='AM': if hour<10: print("0{}:{}".format(str(hour),str(minute))) else: print("{}:{}".format(str(hour),minute )) elif ch=='PM': if hour==12: print("00:{}".format(str(minute))) else: print("{}:{}".format(str(hour%12+12),minute))
23rd Dec 2019, 8:13 PM
Bruce
Bruce - avatar
7th Jan 2020, 7:17 PM
👨🏼‍💻Walter💻
👨🏼‍💻Walter💻 - avatar
+ 1
Я понимаю что оставляю готовое решение, но извините что за задание у которого нет нормального решения. Что за неизвестное условие в тестах? И кстати если человек ищет тут готовый ответ не поломав голову самостоятельно, то значит он такой себе программист. Я 4 дня бьюсь с этим заданием.
16th Jan 2020, 11:56 AM
Александр
+ 1
words = str(input()) words = words.split(":", maxsplit=1) a= int (words[0]) b= words [1] if b[3]=="P" and a==12 : print ("12", b[0:2], sep=':') elif b[3]=="A" : print (words[0], b[0:2], sep=':') elif b[3]=="A" and a==12: print ("00", b[0:2], sep=':') elif b[3]=="P" : print (int (a+12), b[0:2], sep=':')
3rd Apr 2020, 5:49 PM
Roman Buchtajev
Roman Buchtajev - avatar
+ 1
I solved the problem with this python code. Any suggestions for improve?? t=input() st=t.split() fmt=st[0].split(":") if(st[1]=="PM"): if(int(fmt[0])==12): fmt[0]='00' else: fmt[0]=int(fmt[0])+12 else: if(int(fmt[0])==12): fmt[0]='00' elif(len(str(fmt[0]))<2): fmt[0]='0'+str(fmt[0]) print(fmt[0],end=":") print(fmt[1])
22nd Apr 2020, 6:10 PM
Narada Abeykoon
Narada Abeykoon - avatar