HackerRank Library Fine Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Library Fine Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Library Fine Solution
HackerRank Library Fine Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Library Fine Solution

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.

Charges are based only on the least precise measure of lateness. For example, whether a book is due January 1, 2017 or December 31, 2017, if it is returned January 1, 2018, that is a year late and the fine would be 10,000 Hackos.

d1m1y1 = 14, 7, 2018
d2, m2y2 = 5, 7, 2018

The first values are the return date and the second are the due date. The years are the same and the months are the same. The book is 14 – 5 = 9 days late. Return 9 * 15 = 135.

Function Description

Complete the libraryFine function in the editor below.

libraryFine has the following parameter(s):

  • d1, m1, y1: returned date day, month and year, each an integer
  • d2, m2, y2: due date day, month and year, each an integer

Returns

  • int: the amount of the fine or 0 if there is none

Input Format

The first line contains 3 space-separated integers, d1, m1, y1, denoting the respective daymonth, and year on which the book was returned.
The second line contains 3 spaceseparated integers, d2, m2, y2, denoting the respective daymonth, and year on which the book was due to be returned.

Constraints

  • 1 <= d1, d2 <= 31
  • 1 <= m1, m2 <= 12
  • 1 <= y1, y2 <= 3000
  • It is guaranteed that the dates will be valid Gregorian calendar dates.

Sample Input

9 6 2015
6 6 2015

Sample Output

45

Explanation

Given the following dates:
Returned: d1 = 9, m1 = 6, y1 = 2015
Due: d2 = 6, m2 = 6, y2 = 2015

Because y2 = y1, we know it is less than a year late.
Because m2 = m1, we know it’s less than a month late.
Because d2 < d1, we know that 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 (# days late). We then print the result of 15 x (d1 – d2) = 15 x (9 – 6) = 45 as our output.

HackerRank Library Fine Solution

Library Fine Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    
    int edd, emm, eyyyy, add, amm, ayyyy;
    int fine =0;
    
    scanf("%d%d%d%d%d%d", &add, &amm, &ayyyy, &edd, &emm, &eyyyy);
    
    if(ayyyy < eyyyy){
        fine = 0;    
    }
    else if(ayyyy > eyyyy){
        fine = 10000;
    }
    else if(ayyyy == eyyyy){
        if(amm < emm){
            fine = 0;
        }
        else if(amm > emm){
            fine = 500 * (amm - emm);
        }
        else if(amm == emm){
            if(add <= edd){
                fine = 0;
            }
            else if(add > edd){
                fine = 15 * (add - edd);
            }
        }
    }
    
    printf("%d", fine);
    
    return 0;
}

Library Fine Solution in Cpp

#include <bits/stdc++.h>
using namespace std;
int a[1000][1000];
int main()
{
    int n,i,j,k,d1,d2,m1,m2,y1,y2;
    cin>>d1>>m1>>y1;
    cin>>d2>>m2>>y2;
    if((y2-y1>0)|| (y2==y1 && (m2-m1)>0) || (y2==y1 && m2==m1 && d2>=d1))
    cout<<"0"<<endl;
    
    else if(y1!=y2)
    {
        cout<<"10000"<<endl;
    }
    else if(m1!=m2)
    {
        cout<<abs(m1-m2)*500<<endl;
    }
    else if(d1!=d2)
    {
         cout<<abs(d1-d2)*15<<endl;
    }
  
    return 0;
}

Library Fine Solution in Java

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 sc = new Scanner(System.in);
        int actualDay = sc.nextInt();
        int actualMonth = sc.nextInt();
        int actualYear = sc.nextInt();
        int expectedDay = sc.nextInt();
        int expectedMonth = sc.nextInt();
        int expectedYear = sc.nextInt();        
        System.out.println(calcFine(actualDay, actualMonth, actualYear, expectedDay, expectedMonth, expectedYear));
    }
    
    private static int calcFine(int actualDay, int actualMonth, int actualYear, int expectedDay, int expectedMonth, int expectedYear){
        if(actualYear < expectedYear){
            return 0;
        }
        if(actualYear > expectedYear){
            return 10000;
        }
        if(actualMonth < expectedMonth){
            return 0;
        }
        if(actualMonth > expectedMonth){
            return 500 * (actualMonth - expectedMonth);
        }
        
        if(actualDay < expectedDay){
            return 0;
        }
        if(actualDay > expectedDay){
            return 15 * (actualDay - expectedDay);
        }
        return 0;
    }
}
Ezoicreport this ad

Library Fine Solution in Python

import sys
return_date = map(int, sys.stdin.readline().split(' '))
expected_date = map(int, sys.stdin.readline().split(' '))
if return_date[2] > expected_date[2]:
    print '10000'
elif return_date[1] > expected_date[1] and return_date[2] >= expected_date[2]:
    print str((return_date[1] - expected_date[1]) * 500)
elif return_date[0] > expected_date[0] and return_date[2] >= expected_date[2] and return_date[1] >= expected_date[1]:
    print str((return_date[0] - expected_date[0]) * 15)
else:
    print '0'

Library Fine Solution using JavaScript

function processData(input) {
    //Enter your code here
    var input = input.split('\n');
    var actual = input[0].split(' ');
    var expected = input[1].split(' ');
    var fine=0;
    if(parseInt(actual[1]) > parseInt(expected[1])  && parseInt(actual[2]) == parseInt(expected[2])){
       fine = 500 * parseInt(actual[1] - expected[1], 10);
    }
     if(parseInt(actual[2]) > parseInt(expected[2])){
       fine = 10000;
    }
       
    if(parseInt(actual[1]) == parseInt(expected[1]) && parseInt(actual[0]) > parseInt(expected[0]) ){
        fine = 15 * parseInt(actual[0] - expected[0], 10);
    }
    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);
});

Library Fine Solution in Scala

object Solution {
    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
        val actual = readLine.split(" ").map(_.toInt)
        val expected = readLine.split(" ").map(_.toInt)
        val difference = (actual(0) - expected(0), actual(1) - expected(1), actual(2) - expected(2))
        if (difference._3 < 0) {
            println(0)
        } else if (difference._3 == 0) {
            if (difference._2 < 0) {
                println(0)
            } else if (difference._2 == 0) {
                if (difference._1 <= 0) {
                    println(0)
                } else {
                    println(15 * difference._1)
                }
            } else {
                println(500 * difference._2)
            }
        } else {
            println(10000)
        }
    }
}

Ezoicreport this adLibrary Fine Solution in Pascal

program library_fine;
var
    eD,aD:1..31;
    eM,aM:1..12;
    eY,aY:1..3000;
    fine:integer;
begin
    fine:=0;
    ReadLn(aD,aM,aY);
    ReadLn(eD,eM,eY);
    if aY > eY then
        fine:=10000
    else if (aM > eM) and (aY=eY) then
        fine:=(aM-eM)*500
    else if (aD > eD) and (aY=eY) and (aM=eM) then
        fine:=(aD-eD)*15;
    WriteLn(fine);
end.

Disclaimer: This problem (Library Fine) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Repeated String Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad