+ 1
[Solved] (Military Time Code Coach Python, what's wrong? (TY all, too many best answers)
(BTW:I ran this on Python elsewhere and it worked) What am I doing wrong? I'm not familiar with the 12 number system, I realized what I first did was the Japanese AM/PM system, where when it's 00:15, that would be 12:15 PM. Then I looked up the system. I adjusted it to the American. To no avail. Could somebody take a look, please? (12 hour system to be converted to 24. Input hour, space, AM or PM - - > 1:15 PM, output: 13:15) https://code.sololearn.com/cIyv3jwJS7Jk/?ref=app
23 RĂ©ponses
+ 6
Yep. I've spent a lot of time here working on "garbage in garbage out" contingency.
I guess it is only after my first version of logic fails to satisfy all the test cases, so perhaps I am writing robust code those other times, or perhaps I'm just lucky.
It seems to be not intentional garbage, but rather things that a human would easily deal with, eg single letter strings, or a leading zero.
Change:
#elif dorn == 'PM' and int(t[0:2])>=10 and int(t[0:2])<12:
To become:
elif dorn == 'PM' and int(t[0:2])<12:
+ 6
Add: "else: print(t)" đ
+ 6
Korkunç TheTerrible , you asked me for advice.
So, the first thing you should be interested in is what is "Military time" and if you googled, you would find out that 12:00 AM is 00:00.
All other changes happen in PM.
So, you should have only two conditions for changing the time output.
if ... elif ... [else]. âșïž
+ 4
Input 01:30 PM incorrectly outputs as 01:30 using your code.
{Edit: A way to improve the chances of help to a Q&A question is to copy+paste your code into a SoloLearn "code bit", then attach that to your original question (edit, then use the plus icon). We can then run/debug your code.}
+ 3
Um.... You could try it & see how it responds to the 5 test cases. Not saying it is perfect, but perfection can be a roadblock to completed.
{Edit: seriously, just try that change, it is enough to pass the 5 tests đ}
+ 3
But I had that code before, it's why I had to change it, although I changed couple of other stuff following that, so maybe if I change it back it's gonna work. Sounds like you've already tried it? Thanks a bunch btw for sharing your time to help me with this.
+ 3
Korkunç TheTerrible, HungryTradie is giving good recommendations. I ran an exhaustive test that compared the results from your code against my own (which passes all tests). The result gave me hard evidence that Test #4 does indeed input a leading zero in the input hour field, as that was the only comparison where ours handled it differently.
I recommend converting the input hour into an integer up front, and then do numeric comparisons instead of string comparisons. You can leave the minutes in string form, as they do not change from input to output.
+ 3
Korkunç TheTerrible thank you for the temporary "best answer" mark. I agree with final award to HungryTradie, having put the most effort to help.
Sololearn has a number of Code Coach tasks that could use refinement, not to mention course quizzes and challenge questions. And when they make fixes, sometimes it causes new failure in users' code that once passed all the tests. đ€·ââïž
It is free training, so it is fair to hold them to low expectations. They do welcome your feedback. If you care to help them improve, please send email to info@sololearn.com. I believe there is also a feedback option in the app menu.
+ 2
Brian OK but this raises another question for me about code coach's expectations when they provide us with sample input:
Isn't what they expect exactly what they use in their example?
Their sample input was 1:15 and not 01:15. Doesn't that mean that's what they are gonna do in their test inputs(the hour hand without zero if up until 10)?
See, if thatâs not the case, then it means I need to stop the user from entering letters or symbols etc too, because they too then come with the territory? (Sorry, I don't mean to drag, I'm just trying to figure out a rule that I feel soars past over my head as to how rigid the sample inputs are. )
+ 1
Solo Thank you :-) . One of the hidden case tests still fail though.
Is it the case that it is inherently flawed? The python app I have runs it smoothly and gives the desired results , I mean I wasn't able to input an hour whose incorrect output would clue me in on my problem. Do you advise that I try something better? (I am a beginner)
+ 1
G'day Korkunç TheTerrible have you found the search bar in SoloLearn Q&A (it is the magnifying glass icon at the top of the discuss screen).
Many many people think there is something wrong with the test cases, all of them find that their code doesn't respond to something or other. Perhaps your solution will be in the answers to those similar questions?
If you do find your Q&A question solved, then you can be a champion of the forum and edit your question to start with [Solved] so others might find yours when they search.
https://www.sololearn.com/discuss/333866/?ref=app
+ 1
HungryTradie Thank you. AFAIK, I cannot see comments to such exercises until I solve them? I'll check that again.
I don't think I'm being misled by these tests, btw. I am just saying that because it output all the input I had tried in the intended manner(on some Python app), I don't even have a clue how to find out about the problem by myself.
+ 1
No worries mate.
Debugging: I find it important to read the instructions again (a few more times usually!!) To see if they are hiding something in plain sight. Are they sending an invalid input, are they sending a blank, are they doing something weird....
Then I copy my code to somewhere are try to think like a teacher. I send normal inputs, eg 11:30AM or 5:45PM then I send some strange ones like 05:45AM or 1:00AM or 13:00PM just to see what happens.
I'll have a look at my solution and see what I left in my code.
+ 1
I'll go over the code next time I open SL with everything you've said here in mind. I'll have to hit the Zzz world. Thank you for your help.
+ 1
I had a few saved in a few languages, looks like my C code didn't need to check for invalid input (other version checked for hours>12 or for minutes>59) looks like you just need to get it to cope with AM/PM and 1 or 2 digit hours, then correctly change 12:00AM to be 00:00.
I checked for P, then used hours+12 if P, then modulo 24 to bring it back to 0to23, then formatted my output to have 2 digits for hours.
Um... Not sure how that could have worked for 12:00AM....
+ 1
HungryTradie if I do that then when the input is entered in the x:xx am/pm mode it'll turn up error, as the string index changes accordingly. I think I'll have to start from scratch for this.
+ 1
I'm sorry for moving around the best answer tick. Seriously, you are all the best to me. Anyone who shares what they know is a champ.
+ 1
OK, thank you for everything. This clears up for me that there might be minor problems with the sample inputs, and within the limits of common sense, it's OK to try to tweak our codes for better results just in case. That is very good to know for a noob like me actually because that defines how my approach should be, I really wouldn't have known otherwise(i. e I'd assumed they didn't input 0X:XX format in any of their test cases, I simply don't have the confidence to think they did, lol :-))
All the best for you teachers.
+ 1
It works now, but due to my laughably little knowledge of the things one could do with python, I was unable to just use time as an int type, I mean I did remove the ":" in between but it just wasn't going to help if 0x:xx were put in. that and the necessity to use mod 12 etc. This exercise has more to it than it lets on, IMO. Some things seem practical, and then I realize I cannot really do it when there's this other thing too and what's practical for A becomes impractical for B and so on....
https://code.sololearn.com/cQ1bXL34AigA/?ref=app
+ 1
Can you read C code? This logic passes the test cases too.
fgets(iw,9,stdin); #//take the input as a string of 8 including colon and space
sscanf(iw, "%d:%d %s", &hh, &mm, am); #//convert the string into 2 integer variables and a string
hh=hh%12;
#// modulo 12 to give 00: hours
if (am[0]=='P' || am[0]=='p') #// simplistic check for PM.
hh+=12; #//adds 12 if it was a PM or pm
printf("%02d:%02d",hh,mm); #// %02d to give leading zero if required.