HackerRank Day 26 Solution in Python: Nested Logic

Today we will see the HackerRank Day 26 Solution in Python. The problem is named Nested Logic which is part of 30 Days of code on HackerRank. Let’s get started!

Day 26: Nested Logic Problem statement

We are given the expected and actual return dates for a library book, and we have to create a program that calculates the fine (if any). The fee structure is as follows:

  1. If the book is returned on or before the expected return date, no fine will be charged (i.e.:fine=0)
  2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine=15 hackos X number of days late.
  3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine=500 hackos X number of days late.
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000.

Sample Input

9 6 2015    
6 6 2015    

Sample Output

45

You can solve the problem here.

HackerRank Day 26 Solution in Python

#Get the input
d1, m1, y1 = map(int, input().split())
d2, m2, y2 = map(int, input().split())

#If returned year is less than due year
if y1 < y2:
    print(0)
    
#If returned year is equal to due year
elif y1 == y2:
    #If returned month is less than due month
    if m1 < m2:
        print(0)
    #If returned month is equal to due month
    elif m1 == m2:
        #If returned day is less than due day
        if d1 <= d2:
          print(0)
        
        else:
            print(15*(d1-d2))
    else:
        print(500*(m1-m2))
else:
    print(10000)

Code Explanation

  • First, get the two input dates and split them into the day, month, and year
  • d1, m1, y1 corresponds to the returned date and d2,m2,y2 corresponds to the due date
  • Then check the respective conditions given in the problem statement and print the output

Also Read:

Share:

Author: Keerthana Buvaneshwaran