HackerRank Library Fine Problem Solution
In this Post, We will solve HackerRank Library Fine Problem Solution.
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 (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 × (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.
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.
Example
d1, ml, y1 = 14, 7, 2018
d2, m2, y2 = 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 day, month, and year on which the book was returned.
The second line contains 3 space-separated integers, d2, 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
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 × (# days late).
We then print the result of 15 × (d1 – d2) = 15 × (9 − 6) = 45 as our output.
Library Fine C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int ac_day,ac_month,ac_year,ex_day,ex_month,ex_year;
scanf("%d %d %d\n%d %d %d",&ac_day,&ac_month,&ac_year,&ex_day,&ex_month,&ex_year); //ac: actual, ex: expected
if(ac_year>ex_year)
puts("10000");
else if(ac_year<ex_year)
putchar('0');
else if(ac_month>ex_month)
printf("%d",500*(ac_month-ex_month));
else if(ac_month<ex_month)
putchar('0');
else if(ac_day>ex_day)
printf("%d",15*(ac_day-ex_day));
else
putchar('0');
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Library Fine C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int AD, AM, AY;
cin >> AD >> AM >> AY;
int ED, EM, EY;
cin >> ED >> EM >> EY;
if (AY - EY > 0)
{
cout << 10000 << endl;
return 0;
}
else if (AY - EY == 0)
{
if (AM - EM > 0)
{
cout << 500*(AM-EM) << endl;
return 0;
}
else if (AM - EM == 0)
{
if (AD - ED > 0)
{
cout << 15*(AD-ED) << endl;
return 0;
}
}
}
cout << 0 << endl;
return 0;
}
Library Fine C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
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 */
string actual = Console.ReadLine();
string[] actualSplitter = actual.Split(' ');
int actualDay = Convert.ToInt32(actualSplitter[0]);
int actualMonth = Convert.ToInt32(actualSplitter[1]);
int actualYear = Convert.ToInt32(actualSplitter[2]);
string expected = Console.ReadLine();
string[] expectedSplitter = expected.Split(' ');
int expectedDay = Convert.ToInt32(expectedSplitter[0]);
int expectedMonth = Convert.ToInt32(expectedSplitter[1]);
int expectedYear = Convert.ToInt32(expectedSplitter[2]);
if (actualYear > expectedYear)
Console.WriteLine("10000");
else if (actualYear < expectedYear)
Console.WriteLine("0");
else
{
if (actualMonth < expectedMonth)
Console.WriteLine("0");
else if (actualMonth > expectedMonth)
Console.WriteLine(500 * (actualMonth - expectedMonth));
else
{
if (actualDay <= expectedDay)
Console.WriteLine("0");
else if (actualDay > expectedDay)
Console.WriteLine(15 * (actualDay - expectedDay));
}
}
}
}
Library Fine Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import java.time.LocalDate;
class Result {
/*
* Complete the 'libraryFine' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER d1
* 2. INTEGER m1
* 3. INTEGER y1
* 4. INTEGER d2
* 5. INTEGER m2
* 6. INTEGER y2
*/
public static int libraryFine(int d1, int m1, int y1, int d2, int m2, int y2) {
return helper(LocalDate.of(y1, m1, d1), LocalDate.of(y2, m2, d2));
}
static int helper(LocalDate returnDate, LocalDate dueTime) {
if (!returnDate.isAfter(dueTime)) {
return 0;
} else {
if (returnDate.getYear() - dueTime.getYear() > 0) {
return 10000;
} else if (returnDate.getMonthValue() - dueTime.getMonthValue() > 0) {
return (returnDate.getMonthValue() - dueTime.getMonthValue()) * 500;
} else {
return (returnDate.getDayOfMonth() - dueTime.getDayOfMonth()) * 15;
}
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int d1 = Integer.parseInt(firstMultipleInput[0]);
int m1 = Integer.parseInt(firstMultipleInput[1]);
int y1 = Integer.parseInt(firstMultipleInput[2]);
String[] secondMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int d2 = Integer.parseInt(secondMultipleInput[0]);
int m2 = Integer.parseInt(secondMultipleInput[1]);
int y2 = Integer.parseInt(secondMultipleInput[2]);
int result = Result.libraryFine(d1, m1, y1, d2, m2, y2);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Library Fine JavaScript Solution
function processData(input) {
var input = input.split('\n');
var actual = input[0].split(' ');
var expected = input[1].split(' ');
var years = actual[2] - expected[2];
var months = actual[1] - expected[1];
var days = actual[0] - expected[0];
if(years > 0) {
console.log(10000);
} else if(years == 0 && months > 0) {
console.log(months * 500);
} else if(months == 0 && days > 0) {
console.log(days * 15)
} else {
console.log(0);
}
}
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 Python Solution
import os
import sys
import re
def main():
retDay, retMonth, retYear = map( (int), input().split() )
day, month, year = map( (int), input().split() )
# Calculate fine
if year < retYear:
fine = 10000
elif year > retYear:
fine = 0
elif month < retMonth:
fine = 500 * ( retMonth - month )
elif month > retMonth:
fine = 0
elif day < retDay:
fine = 15 * ( retDay - day )
else:
fine = 0
print( fine )
if __name__ == "__main__":
main()
other solutions