Today we are going to solve HackerRank Day 26: Nested Logic 30 days of code solution in C++, Java, javascript & Python.
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:
- If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0.
- 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).
- 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).
- 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 day, month, 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 (D1 – D2) = 15 x (9 – 6) = 45 as our output.
HackerRank Day 26: Nested Logic 30 days of code solution
Nested Logic HackerRank Solution in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int returned[3] = {0};
int expected[3] = {0};
scanf("%d %d %d\n", &returned[0], &returned[1], &returned[2]);
scanf("%d %d %d\n", &expected[0], &expected[1], &expected[2]);
if(returned[2] > expected[2]) {
printf("10000\n");
} else if (returned[1] > expected[1]) {
int months_late = returned[1] - expected[1];
printf("%d\n", months_late * 500);
} else if (returned[0] > expected[0]) {
int days_late = returned[0] - expected[0];
printf("%d\n", days_late * 15);
} else {
printf("0\n");
}
return 0;
}
Nested Logic HackerRank Solution in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int d1;
int m1;
int y1;
cin >> d1 >> m1 >> y1;
int d2;
int m2;
int y2;
cin >> d2 >> m2 >> y2;
if(y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 <= d2)){
cout << 0 << endl;
}
else if(m1 == m2 && y1 == y2){
cout << 15*(d1 - d2) << endl;
}
else if(y1 == y2){
cout << 500*(m1 - m2) << endl;
}
else {
cout << 10000*(y1 - y2) << endl;
}
return 0;
}
Nested Logic HackerRank Solution in Java
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int fine;
Scanner scan = new Scanner(System.in);
int returnDay = scan.nextInt();
int returnMonth = scan.nextInt();
int returnYear = scan.nextInt();
int dueDay = scan.nextInt();
int dueMonth = scan.nextInt();
int dueYear = scan.nextInt();
scan.close();
if(returnYear <= dueYear){
if(returnMonth <= dueMonth){
if(returnDay <= dueDay){
fine = 0;
}else{
fine = (returnDay - dueDay) * 15;
}
}else{
fine = (returnMonth - dueMonth) * 500;
}
}else{
fine = 10000;
}
System.out.println(fine);
}
}
Nested Logic HackerRank Solution in Python 2
# Enter your code here. Read input from STDIN. Print output to STDOUT
[da, ma, ya] = [int(x) for x in raw_input().strip().split(" ")]
[de, me, ye] = [int(x) for x in raw_input().strip().split(" ")]
if ya > ye:
print 10000
elif ma > me:
print 500 * (ma - me)
elif da > de:
print 15 * (da - de)
else:
print 0
Nested Logic HackerRank Solution in Python 3
n = input()
x = list(map(int, n.split()))
m = input()
y = list(map(int, m.split()))
z=0
if y[2] < x[2]:
z = 10000
elif y[2] == x[2]:
if y[1] < x[1]:
z = 500*(x[1]-y[1])
elif y[0] < x[0]:
z = 15*(x[0]-y[0])
print(z)
Nested Logic HackerRank Solution in Javascript
function processData(input) {
//Enter your code here
var inputArr = input.split('\n'),
actual = inputArr[0].split(' '),
expected = inputArr[1].split(' '),
month = 1,
day = 0,
year = 2,
fine = 0;
if (actual[year] > expected[year]) {
fine = 10000;
} else if (actual[month] > expected[month]) {
fine = 500 * (actual[month] - expected[month]);
} else if (actual[day] > expected[day]) {
fine = 15 * (actual[day] - expected[day]);
}
console.log(fine);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});