• + 15 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

    • + 3 comments

      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.

      • + 2 comments

        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.

        • + 1 comment

          I am having the same problem

          • + 1 comment

            As am I...Anyone have any luck?

            • + 3 comments

              Check your assumptions about return dates.

              • + 0 comments

                ok i soglasyen do you speak russian

              • + 0 comments

                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.

        • + 0 comments

          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

      • + 1 comment

        i passed all test cases..so test cases do not have any problem you can comment your code then we will review on that

        • + 1 comment

          import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

          public class Solution {

          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              int d1 = in.nextInt();
              int m1 = in.nextInt();
              int y1 = in.nextInt();
              int d2 = in.nextInt();
              int m2 = in.nextInt();
              int y2 = in.nextInt();
              int fine;
              if(y2 == y1 || y1 < y2){
                  if(m2 == m1 || m1 < m2){
                      if(d2 == d1 || d1 < d2){
                          fine = 0;
                      }
                      else
                          fine = (d1 - d2) * 15;
                  }
                  else
                      fine = (m1 - m2) * 500;
              }
              else
                  fine = 10000;
              System.out.println(fine);
          }
          

          }

          • + 1 comment

            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

      • [deleted]
        + 4 comments

        This one, in Python, flows smoothly in all the test cases:

        def libraryFine(d1, m1, y1, d2, m2, y2):
            fine = 0
            if y1>y2:
                fine = 10000
            elif y1<=y2:
                if m1>m2 and (y1>=y2):
                    fine = 500 * (m1-m2)
                elif m1<=m2:
                    if (d1>d2 and (m1-m2>=0) and (y1>=y2)):
                        fine = 15*(d1-d2)
                    else:
                        fine = 0
            return fine
        
        • + 1 comment

          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

          • + 2 comments

            @michal_remis Agreed!

            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
            
            • + 0 comments

              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.

            • + 0 comments

              thanks man... @tat_lim i'm just screwed up with this logic problem :)

        • + 6 comments

          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:

          def libraryFine(d1, m1, y1, d2, m2, y2):
              if y1<y2:
                  return 0
              if y1>y2:
                  return 100000
                  
              if m1<m2:
                  return 0
              if m1>m2:
                  return (m1-m2)*500
                  
              return (d1-d2)*15
          

          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_code
          if condition:
              return bailout_value
          more_code
          

          The code I wrote is an extreme example of it. Mind the blank lines which provide extra information!

          • + 0 comments

            @programaths Agreed!

            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
            
          • + 0 comments

            Same thinking:

            def libraryFine(d1, m1, y1, d2, m2, y2):
                    if y1 > y2:
                            return (y1 - y2) * 10000
                    if y1 < y2:
                            return 0
                    if m1 > m2:
                            return (m1 - m2) * 500
                    if m1 < m2:
                            return 0
                    if d1 > d2:
                            return (d1 - d2) * 15
                    return 0
            
          • + 0 comments

            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;
            
          • + 0 comments

            above fails when month and year are same and d1 is less than d2. Replaced

            return (d1-d2)*15

            with

            return (d1-d2)*15 if (d1 > d2) else 0

          • + 0 comments

            Will it work for the test case

            11.09.2018

            15.09.2018

            Or will the library pay him some Hackos

          • + 1 comment

            what happens if he returns in the same year , same month and before return date ? According to your code (d1-d2) returns negative value.

            • + 0 comments

              Yeah, I think you're right. Lapinpub's code avoids it.

        • + 0 comments

          include this sattement also at last ,with mentioned code avove 4test case does not excutes Coder.. if (y1 < y2) { fine = 0; }

        • + 0 comments

          once if (y1=y2 ? isnt it contradictory statement?

    • + 2 comments

      What is your point? http://inft.ly/48jeZKF

      • + 2 comments

        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

        • + 0 comments

          This may be the most helpful comment ever. ;)

      • + 1 comment

        do we need to consider leap year..number of days in a months..and so on...

        • + 2 comments

          nopes

          • + 1 comment

            So, we have to consider all month with 31 days?

            • + 0 comments

              Doesn't matter anyway.

          • + 1 comment

            These details should have been mentioned in the actual questions.... Laziness doesn't get you anywhere!

            • + 0 comments

              Over complicating things doesn't get us anywhere either. The question needed you to just follow the four statements.

    • + 0 comments

      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 ;)

    • + 0 comments

      Exactly

    • + 1 comment

      I still have the problem ...

      • + 0 comments

        @mtapkan: With test cases 5 and 10 specifically?

    • + 2 comments

      Same problem test case 5th and test case 10th screwed. :(

      • + 0 comments

        yeah

    • + 1 comment

      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.

      • + 0 comments

        Thank you for ur help @Allison :) I figured out my mistake already thanks alot. :)

    • + 0 comments

      Thanks for sharing your info for solving the problem. It finally helped me locate my bad logic.

    • + 1 comment

      same.... checked in custom input, and got the answer correct, but fails in submission....

      • + 0 comments

        true same problem

    • [deleted]
      + 0 comments

      I tried, after several I/O, the following code, which showed no errors:

      def libraryFine(d1, m1, y1, d2, m2, y2):
          fine = 0
          if y1>y2:
              fine = 10000
          elif y1<=y2:
              if m1>m2 and (y1>=y2):
                  fine = 500 * (m1-m2)
              elif m1<=m2:
                  if (d1>d2 and (m1-m2>=0) and (y1>=y2)):
                      fine = 15*(d1-d2)
                  else:
                      fine = 0
          return fine
      
    • + 0 comments

      Guys there is no problem with testcases

      You can use following code

      if y1 < y2 :
          return 0
      elif y1 > y2 :
          return 10000
      else :
          if m1 < m2 :
              return 0
          elif m1 > m2 :
              return (m1-m2)*500
          else :
              if d1 < d2 :
                  return 0
              elif d1 > d2 :
                  return (d1-d2)*15
              else :
                  return 0
      
    • + 0 comments

      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.

    • + 0 comments

      The test cases are correct. You should check your logic.