Day 26: Nested Logic | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 26: Nested Logic HackerRank Solution in C++, Java and Python.

Day 26: Nested Logic

Objective

Today’s challenge puts your understanding of nested conditional statements to the test.

Task

Your local library needs your help! Given the expected and actual return dates for a library book, 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 (the 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 (the number of months late).
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Example
d1, m1, y1 = 12312014  returned date
d2, m2, y2 = 112015 due date

The book is returned on time, so no fine is applied.

d1, m1, y1 = 112015 returned date
d2, m2, y2 = 12312014 due date

The book is returned in the following year, so the fine is a fixed 10000.

Input Format

The first line contains 3 space-separated integers denoting the respective day, month, and year on which the book was actually returned.
The second line contains 3 space-separated integers denoting the respective daymonth, and year on which the book was expected to be returned (due date).

Constraints

  • 1 <= D <= 31
  • 1 <= M <= 12
  • 1 <= Y <= 3000
  • It is guaranteed that the dates will be valid Gregorian calendar dates.

Output Format

Print a single integer denoting the library fine for the book received as input.

Sample Input

STDIN       Function
-----       --------
9 6 2015    day = 9, month = 6, year = 2015 (date returned)
6 6 2015    day = 6, month = 6, year = 2015 (date due)

Sample Output

45

Explanation

Given the following return dates:
Returned: D1 = 9, M1 = 6, Y1 = 2015
Due: D2 = 6, M2 = 6, Y2 = 2015

Because Y2 = Y1, it is less than a year late.
Because M2 = M1, it is less than a month late.
Because D2 < D1, it was returned late (but still within the same month and year).

Per the library’s fee structure, we know that our fine will be 15 Hackos x (#dayslate). We then print the result of 15 x (D1D2) = 15 x (9 – 6) = 45 as our output.

Solution – Day 26: Nested Logic

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
    int Day, Month, Year, Day1, Month1, Year1, mdiff, ddiff, ydiff, diff;
    
    cin>>Day>>Month>>Year>>Day1>>Month1>>Year1;

    mdiff = Month - Month1;
    ddiff = Day - Day1;
    ydiff = Year - Year1;
    diff=(Year-Year1>0)?10000:(Month-Month1>0&&ydiff==0)?mdiff*500:(Day-Day1>0 && ydiff==0)?ddiff*15:0;    
    cout<<diff<<endl;

    return 0;
}

Java

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int day = sc.nextInt();
        int month = sc.nextInt();
        int year = sc.nextInt();
        int dayExpected = sc.nextInt();
        int monthExpected = sc.nextInt();
        int yearExpected = sc.nextInt();
        int fine = 0;
        
        if(year > yearExpected){
            fine = 10000;
        }else if(year == yearExpected && month > monthExpected){
            fine = (month - monthExpected) * 500;
        }else if(year == yearExpected && month == monthExpected && day > dayExpected){
            fine = (day - dayExpected) * 15;
        }
        System.out.println(fine);
    }

}

Python

actual = input()
actual = list(map(int, actual.split(" ")))

expected = input()
expected = list(map(int, expected.split(" ")))

actualDate = actual[0]
actualMonth = actual[1]
actualYear = actual[2]

expectedDate = expected[0]
expectedMonth = expected[1]
expectedYear = expected[2]

fine = 0

if actualYear > expectedYear:
    fine = 10000
elif actualYear == expectedYear: #Same calender year
    if actualMonth > expectedMonth: #checking months
        fine = 500 * (actualMonth - expectedMonth)
    elif actualMonth == expectedMonth: #check dates now
        if actualDate > expectedDate:
            fine = 15 * (actualDate - expectedDate)
print(fine)

Disclaimer: The above Problem (Day 26: Nested Logic) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *