Library Fine HackerRank Solution in C, C++, Java, Python

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 * (the number of days later) .
  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 * (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 10000 Hackos.

Example

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  days late. Return .

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 day,month , and year on which the book was returned.

The second line contains 3 space-separated integers,d1,m2,y2 denoting the respective day,month, and year on which the book was due to be returned.

Sample Input

9 6 2015

6 6 2015

 

Sample Output

45

Library Fine HackerRank 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 HackerRank Solution in C++

#include<iostream>
using namespace std;
int main()
{
    int actual[3],expected[3],i,j;
    for(i=0;i<3;i++)
    cin>>actual[i];
    for(i=0;i<3;i++)
    cin>>expected[i];
   
    if(actual[2]-expected[2]<0)
        cout<<0;
    else if(actual[2]-expected[2]>0)
        cout<<10000;
    else if(actual[1]-expected[1]<0)
        cout<<0;
    else if(actual[1]-expected[1]>0)
        cout<<500*(actual[1]-expected[1]);
    else if(actual[0]-expected[0]>0)
        cout<<15*(actual[0]-expected[0]);
    else
        cout<<0;

}

 

Library Fine HackerRank 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 in = new Scanner(System.in);
        int rtday = in.nextInt();
        int rtmonth = in.nextInt();
        int rtyear = in.nextInt();
        int dueday = in.nextInt();
        int duemonth = in.nextInt();
        int dueyear = in.nextInt();
        int fine = 0;
        if(rtyear > dueyear)
            fine = 10000;
        else if(rtyear == dueyear){
            if(rtmonth > duemonth)
                fine = 500 * (rtmonth - duemonth);
            else if(rtmonth == duemonth){
                if(rtday > dueday)
                    fine = 15 * (rtday - dueday);
            }
        }
        System.out.println(fine);
    }
}

 

Library Fine HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;

class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        var actual = DateTime.ParseExact(
            Console.ReadLine().Replace(' ', '/'), "d/M/yyyy", CultureInfo.InvariantCulture);
        var expected = DateTime.ParseExact(
            Console.ReadLine().Replace(' ', '/'), "d/M/yyyy", CultureInfo.InvariantCulture);
        
        int fine;
        
        if (actual <= expected)
        {
            Console.WriteLine(0);
        }
        else if (actual.Year != expected.Year)
        {
            Console.WriteLine(10000);
        }
        else if (actual.Month != expected.Month)
        {
            Console.WriteLine((actual.Month - expected.Month) * 500);
        }
        else if (actual.Day != expected.Day)
        {
            Console.WriteLine((actual.Day - expected.Day) * 15);
        }
    }
}

 

 

Attempt Library Fine HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/library-fine/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/cut-the-sticks-hackerrank-solution/

 

Leave a Comment