Military time challenge | Sololearn: Learn to code for FREE!

+5

Military time challenge

I wrote this code for military time challenge and I don't understand it does not work for two test cases tform=input().split(":") if "PM" in tform[1] and tform[0] !="12": tform[0]=str(12+int(tform[0])) if tform[0]=="12" and "AM" in tform[1]: tform[0]="24" x,y=tform[0],tform[1] y=("".join(y))[:-3] tt=[x] tt.append(y) print(":".join(tt))

12/25/2019 7:45:03 AM

Fabian Nwobi

22 Answers

New Answer

+16

I've test your code. 4:45 AM becomes 4:45 That's not ok for test cases. It should be 04:45 All hours between 00:xx and 09:xx must have two digits.

+3

Solution here! https://code.sololearn.com/cy9T0LRzvJ9A/?ref=app

+3

I solved the problem with functional programming time = input() time = time.split() def am_converter(time): time = time[0].split(':') if int(time[0]) < 10: time = "0" + time[0] + ':' + time[1] elif int(time[0]) == 12 : time = '00' + ':' + time[1] else: time = time[0] + ':' + time[1] return time def pm_converter(time): time = time[0].split(':') if time[0] == '12': time = time[0] + ':' + time[1] else: time_start = (12 + int(time[0])) time_end = time[1] time = str(time_start) + ":" + time_end return time def military_time(time): if time[1] == "PM": print(pm_converter(time)) else: print(am_converter(time)) military_time(time)

+2

If you have 12 and AM then tform[0] needs to be "00" not 24 else for input like 12:45 AM your output will be 24::45 it should be 00:45

+2

Thanks Thomas. Exactly what I was looking for.

+1

https://code.sololearn.com/cGIeXNvMY073/?ref=app Thanks swim. I have done so, long before you mentioned it, but it still didn't work. I wish I knew all their criteria

+1

Noob your solution doesnt handle 12pm or 12 am properly, here is a correction. (I like your use of replace and indexing) a=input() b=a.replace("AM","").replace ("PM","") ans="" if a[-2:]=="AM": if b[1]==":": print("0"+b[0:]) elif b[:2] == "12": print("00" + b[2:]) else: print(b[0:]) else: if b[1]==":": ans=str(int(b[0])+12)+b[1:] elif b[2]==":" and b[0:2]!="12": ans=str(int(b[0:2])+12)+b[2:] else: ans= b print(ans)

+1

My solution: time = input() hour, pora = time.split(" ") hours, minutes = hour.split(":") def change(time): new_hours = "" if pora == "PM": new_hours = int(hours)+12 elif int(hours) <10: new_hours = "0"+hours else: new_hours = hours return(str(new_hours)+":"+minutes) print(change(time))

+1

https://code.sololearn.com/cyzixTjbv18k/?ref=app

+1

https://code.sololearn.com/c1Riz7b6j8ma/?ref=app

+1

Mr.Dark What about 12:00AM == 00:00

+1

Thanks

+1

https://code.sololearn.com/cL6xAfLUMguq/?ref=app

0

Fabian Nwobi Well I didn't executed your program. I saw that "24" value and knew that was an error. Do you know which test cases are failing or are they hidden?

0

They were hidden

0

SoundInfinity 12:00pm should write 12:00 12:00 am shiuld write 00:00

0

It won't work for 12:XY AM . It is just common sense. 12:XY AM never comes or we can say never exists. So we can ignore that fact. Also, I solved it by using only the very basics. And it worked with all test cases. https://code.sololearn.com/cqOgKoFFGw4m/?ref=app

0

Here is my code in c++ . #include <iostream> using namespace std; int main () { unsigned short int userTime,outputtime ,pre,post; string ampm; cout <<"Enter time(24h): "; cin >>userTime; if(userTime / 1000 == 0 ) { ampm = "am"; outputtime = 1200 - userTime; } else { ampm = "pm"; outputtime = userTime -1200; } pre = outputtime /100; post = outputtime % 100; if(post >=60) { post = post -60; pre = pre +1; } cout <<"Time in 12h system: " <<pre <<":"<<post<<ampm; return 0; }

0

Well forgot about that

0

Debugged my code