Header Ad

HackerRank Library Fine problem solution

In this HackerRank Library Fine problem you have Given the expected and actual return dates for a library book, create a program that calculates the fine.

HackerRank Library Fine problem solution


Problem solution in Python programming.

res = list(reversed([int(x) for x in input().split()]))
due = list(reversed([int(x) for x in input().split()]))

def calc(res, due):
    if res[0] < due[0]:
        return 0
    if res[0] > due[0]:
        return 10000
    if res[1] < due[1]:
        return 0
    if res[1] > due[1]:
        return 500 * (res[1] - due[1])
    if res[2] < due[2]:
        return 0
    if res[2] > due[2]:
        return 15 * (res[2] - due[2])
    return 0
print(calc(res, due))


Problem solution in Java Programming.

import static java.lang.System.out;
import java.util.Scanner;

public class WarmLibraryFine {

    public static void main(String x[]) {
        WarmLibraryFine o = new WarmLibraryFine();
        o.run();
    }

    void run() {
        try (final Scanner in = new Scanner(System.in, "ascii")) {

            final int fine;

            final int aday = in.nextInt();
            final int amon = in.nextInt();
            final int ayr = in.nextInt();

            final int eday = in.nextInt();
            final int emon = in.nextInt();
            final int eyr = in.nextInt();

            if (ayr < eyr) {
                fine = 0;
            } else if (ayr > eyr) {
                fine = 10_000;
            } else if (amon < emon) {
                fine = 0;
            } else if (amon > emon) {
                fine = 500 * (amon - emon);
            } else if (aday > eday) {
                fine = 15 * (aday - eday);
            } else {
                fine = 0;
            }
            out.println(fine);
        }
    }
}


Problem solution in C++ programming.

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


int main() {
    int ed,em,ey,ad,am,ay;
    cin>>ad>>am>>ay;
    cin>>ed>>em>>ey;
    if(ay>ey){
        cout<<10000<<endl;
    }else if((ey==ay)&&(am>em)){
        cout<<500*(am-em)<<endl;
    }else if((ad<=em)&&(am<=em)&&(ay<=ey)||(ay<ey)||(am<em)&&(ay<=ey)){
        cout<<0<<endl;
    }else{
        cout<<15*(ad-ed)<<endl;
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int d1, m1, y1, d2, m2, y2;
    scanf("%d %d %d %d %d %d", &d1, &m1, &y1, &d2, &m2, &y2);
    if (y1 > y2) 
        printf("10000");
    else
        if (y1 < y2)
            printf("0");
        else
            if (m1 > m2)
                printf("%d", (m1-m2)*500);
            else
                if (m1 < m2)
                    printf("0");
                else
                    if (d1 > d2)
                        printf("%d", (d1-d2)*15);
                    else
                        printf("0");
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var dates = input.split('\n').map(function(date) {
        return date.split(' ').map(function(datePart) {
            return parseInt(datePart, 10);
        });
    });
    if (dates[0][2] > dates[1][2]) {
        console.log(10000);
    } else if (dates[0][2] < dates[1][2]) {
        console.log(0);
    } else if (dates[0][1] > dates[1][1]) {
        console.log((dates[0][1] - dates[1][1]) * 500);
    } else if (dates[0][1] < dates[1][1]) {
        console.log(0);
    } else {
        console.log(Math.max(dates[0][0] - dates[1][0], 0) * 15);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments