We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I had problems with the same tests you had, but I actually found an error in my logic. Once fixed, it passed all tests. Try to review your code and check your tests. Maybe you forgot to output data in some specific scenario.
Specifically if it was returned in an earlier year than the due date's year, but in a later month than the due date's month.
Due:May 3, 1985
Returned: March 2, 1984
Be careful you're not returning 30 here (may - march) * 15.
They are correct, I just fixed my logic and it went well.
" else if (m2 < m1)
{
if(y2 > y1)
{
Console.WriteLine(0);
return;
}
....
" in the month condition
"else if (d2 < d1)
{
if(y2 > y1 || m2 > m1)
{
Console.WriteLine(0);
return;
}
...
"in the days condition
def libraryFine(d1, m1, y1, d2, m2, y2):
if y1>y2:
return 10000
if y1==y2:
if m1>m2:
return (m1-m2)*500
if m1==m2 and d1>d2:
return (d1-d2)*15
return 0
I ended up doing the exact same thing, except my method starts with a 'fine' variable equal to zero, and it gets set under the different conditions. Default is to return no fine. I'm not a big fan of multiple returns in a single funciton.
We are "pruning" the possibilities from top to bottom. First we deal with years.
If it's the year before, we know it is 0 and there is nothing more to do.
If it's the year after, we know it is 100000 and there is nothing more to do.
Then we have to deal with months only.
Same story, but now we have a formula to calculate the fine.
When we dealt with month, we only to take care of days.
As you can see, more we go down, less we have to remember! The programe flow and state becomes easier as we go deeper because we prune variables!
If you look at your program, you got confuse and wrote unnecessary conditions and even made them very complex.
Early return and bail out are very powerfull tools.
Usually, it's in the shape:
some_codeifcondition:returnbailout_valuemore_code
The code I wrote is an extreme example of it. Mind the blank lines which provide extra information!
def libraryFine(d1, m1, y1, d2, m2, y2):
if y1>y2:
return 10000
if y1==y2:
if m1>m2:
return (m1-m2)*500
if m1==m2 and d1>d2:
return (d1-d2)*15
return 0
Agreed... that was my solution as well. However, multiple returns from a function are not a good idea in general as they tend to lead towards unmanagable and hard to debug code in the long run. I've done the same here with just a single exit point. The empty braces are "unexpected" but are a hint to the "support programmer" how the code functions... literally, in this case do nothing.
fine = 0;
if ( y1 < y2 ) {}
else if ( y1 > y2 ) { fine = 10000; }
else if ( m1 < m2 ) { }
else if ( m1 > m2 ) { fine = ( m1 - m2 ) * 500; }
else if ( d1 > d2 ) { fine = ( d1 - d2 ) * 15; }
return fine;
All this while I thought that the output I see when I spend the hackos was my program's output. Thanks to a friend I realized now that, that is the correct output. Really sorry.
BTW found the bug :D
I also initially had problems w/ those 2 test cases, found bugs in my logic, and now all tests pass. So, at least as of this time, there are no bugs in the tests ;)
Those cases make logical sense, as both of the actual return dates occur before the expected return dates. I can't see your solution, but it sounds like there is a problem with the way you're nesting your conditional logic. You should be checking the year, then the month within the year, then the day within the month.
Library Fine
You are viewing a single comment's thread. Return to all comments →
I think the fifth and tenth cases are screwed up. Please check. All else I got correct answers. Here's my solution: https://www.hackerrank.com/challenges/library-fine/submissions/code/12064663
5th case: Input: 2 7 1014 1 1 1015 Output: 0 As expected, returned before expected date
10th case: Input: 28 2 2015 15 4 2015 Output: 0 Again, as expected
I had problems with the same tests you had, but I actually found an error in my logic. Once fixed, it passed all tests. Try to review your code and check your tests. Maybe you forgot to output data in some specific scenario.
I spent 10 hackos, got both the test cases, checked I/O. So I'm sure that the output is in expected format and correct.
I am having the same problem
As am I...Anyone have any luck?
Check your assumptions about return dates.
ok i soglasyen do you speak russian
Specifically if it was returned in an earlier year than the due date's year, but in a later month than the due date's month.
Due:
May 3, 1985
Returned:
March 2, 1984
Be careful you're not returning 30 here (may - march) * 15.
They are correct, I just fixed my logic and it went well. " else if (m2 < m1) { if(y2 > y1) { Console.WriteLine(0); return; } .... " in the month condition
"else if (d2 < d1) {
if(y2 > y1 || m2 > m1) { Console.WriteLine(0); return; } ... "in the days condition
i passed all test cases..so test cases do not have any problem you can comment your code then we will review on that
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}
for example if the case is actual 12 03 2014 expected 11 03 2015
then o/p : 0 but ur code returns 15
And same for the case actual 12 04 2014 expected 12 03 2015 then o/p: 0 but ur code returns 500
This one, in Python, flows smoothly in all the test cases:
I thing your conditions are too complicated.. e.g. you check both y1<=y2 and y2<=y1.. why not just check y1==y2 ? Same with m1,m2
@michal_remis Agreed!
I ended up doing the exact same thing, except my method starts with a 'fine' variable equal to zero, and it gets set under the different conditions. Default is to return no fine. I'm not a big fan of multiple returns in a single funciton.
thanks man... @tat_lim i'm just screwed up with this logic problem :)
You can avoid all the "else" by bailing out. The statement that one should avoid multiple "return" do not always hold true and is not alway wize.
Here is an axample:
We are "pruning" the possibilities from top to bottom. First we deal with years. If it's the year before, we know it is 0 and there is nothing more to do. If it's the year after, we know it is 100000 and there is nothing more to do.
Then we have to deal with months only. Same story, but now we have a formula to calculate the fine.
When we dealt with month, we only to take care of days.
As you can see, more we go down, less we have to remember! The programe flow and state becomes easier as we go deeper because we prune variables!
If you look at your program, you got confuse and wrote unnecessary conditions and even made them very complex.
Early return and bail out are very powerfull tools. Usually, it's in the shape:
The code I wrote is an extreme example of it. Mind the blank lines which provide extra information!
@programaths Agreed!
Same thinking:
Agreed... that was my solution as well. However, multiple returns from a function are not a good idea in general as they tend to lead towards unmanagable and hard to debug code in the long run. I've done the same here with just a single exit point. The empty braces are "unexpected" but are a hint to the "support programmer" how the code functions... literally, in this case do nothing.
above fails when month and year are same and d1 is less than d2. Replaced
with
Will it work for the test case
11.09.2018
15.09.2018
Or will the library pay him some Hackos
what happens if he returns in the same year , same month and before return date ? According to your code (d1-d2) returns negative value.
Yeah, I think you're right. Lapinpub's code avoids it.
include this sattement also at last ,with mentioned code avove 4test case does not excutes Coder.. if (y1 < y2) { fine = 0; }
once if (y1=y2 ? isnt it contradictory statement?
What is your point? http://inft.ly/48jeZKF
All this while I thought that the output I see when I spend the hackos was my program's output. Thanks to a friend I realized now that, that is the correct output. Really sorry. BTW found the bug :D
This may be the most helpful comment ever. ;)
do we need to consider leap year..number of days in a months..and so on...
nopes
So, we have to consider all month with 31 days?
Doesn't matter anyway.
These details should have been mentioned in the actual questions.... Laziness doesn't get you anywhere!
Over complicating things doesn't get us anywhere either. The question needed you to just follow the four statements.
I also initially had problems w/ those 2 test cases, found bugs in my logic, and now all tests pass. So, at least as of this time, there are no bugs in the tests ;)
Exactly
I still have the problem ...
@mtapkan: With test cases 5 and 10 specifically?
Same problem test case 5th and test case 10th screwed. :(
yeah
Those cases make logical sense, as both of the actual return dates occur before the expected return dates. I can't see your solution, but it sounds like there is a problem with the way you're nesting your conditional logic. You should be checking the year, then the month within the year, then the day within the month.
Thank you for ur help @Allison :) I figured out my mistake already thanks alot. :)
Thanks for sharing your info for solving the problem. It finally helped me locate my bad logic.
same.... checked in custom input, and got the answer correct, but fails in submission....
true same problem
I tried, after several I/O, the following code, which showed no errors:
Guys there is no problem with testcases
You can use following code
My code passed all tests, so the problem wasn't the issue. Btw, I've seen your code, but I don't program in Pyton, so I can't help it, sorry.
The test cases are correct. You should check your logic.