Algorithm


Problem Name: 30 days of code - Day 26: Nested Logic

Problem Link: https://www.hackerrank.com/challenges/30-nested-logic/problem?isFullScreen=true

Objective
Today's challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, but check out the Tutorial tab for a video on testing.

 

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  * (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  * (the number of monthes late).
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Constraints

  • 1 <= D <= 31
  • 1 <= M <= 12
  • 1 <= Y <= 3000
  • It is guarenteed that the dates will be valid Gregorian calendar dates.

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

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#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 Aday, Amonth, Ayear, Eday,Emonth, Eyear;
    int fine = 0;
    scanf("%d %d %d %d %d %d", &Aday, &Amonth, &Ayear, &Eday, &Emonth, &Eyear);
    if (Ayear == Eyear){
        if (Amonth == Emonth){
            fine = 15 * (Aday - Eday);
        }
        else {
            fine = 500 * (Amonth - Emonth);
        }
    }
    else if (Ayear > Eyear){
        fine = 10000;
    }
    
    if (fine  <  0){
        fine = 0;
    }
    
    printf("%d", fine);
    return 0;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main() {
    int da, ma, ya;
    cin >> da;
    cin >> ma;
    cin >> ya;

    int de, me, ye;
    cin >> de;
    cin >> me;
    cin >> ye;

    int fine = 0;

    if (ya > ye) fine = 10000;
    else if (ya == ye) {
        if (ma > me) fine = (ma - me) * 500;
        else if (ma == me && da > de) fine = (da - de) * 15;
    }

    cout << fine;
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


using System;

class Solution
{
    static void Main(String[] args)
    {
        var actually = Console.ReadLine().Split(' ');
        var da = int.Parse(actually[0]);
        var ma = int.Parse(actually[1]);
        var ya = int.Parse(actually[2]);

        var expected = Console.ReadLine().Split(' ');
        var de = int.Parse(expected[0]);
        var me = int.Parse(expected[1]);
        var ye = int.Parse(expected[2]);

        var fine = 0;

        if (ya > ye) fine = 10000;
        else if (ya == ye)
        {
            if (ma > me) fine = (ma - me) * 500;
            else if (ma == me && da > de) fine = (da - de) * 15;
        }

        Console.WriteLine(fine);
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Golang Programming

Code - Golang Programming


package main

import "fmt"

func fineCalculator(actualDay, actualMonth, actualYear, expectedDay, expectedMonth, expectedYear int) int {
	if actualYear < expectedYear {
		return 0
	}
	if actualYear > expectedYear {
		return 10000
	}
	if actualMonth < expectedMonth {
		return 0
	}

	if actualMonth > expectedMonth {
		return (actualMonth - expectedMonth) * 500
	}

	if actualDay < expectedDay {
		return 0
	}

	if actualDay > expectedDay {
		return (actualDay - expectedDay) * 15
	}
	return 0
}

func main() {
	var actualDay, actualMonth, actualYear int
	var expectedDay, expectedMonth, expectedYear int
	fmt.Scan(&actualDay)
	fmt.Scan(&actualMonth)
	fmt.Scan(&actualYear)

	fmt.Scan(&expectedDay)
	fmt.Scan(&expectedMonth)
	fmt.Scan(&expectedYear)

	result := fineCalculator(actualDay, actualMonth, actualYear, expectedDay, expectedMonth, expectedYear)
	fmt.Println(result)
}
Copy The Code & Try With Live Editor

#5 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int da = in.nextInt();
        int ma = in.nextInt();
        int ya = in.nextInt();

        int de = in.nextInt();
        int me = in.nextInt();
        int ye = in.nextInt();

        int fine = 0;

        if (ya > ye) fine = 10000;
        else if (ya == ye) {
            if (ma > me) fine = (ma - me) * 500;
            else if (ma == me && da > de) fine = (da - de) * 15;
        }

        System.out.println(fine);
    }
}

Copy The Code & Try With Live Editor

#6 Code Example with Javascript Programming

Code - Javascript Programming


function processData(input) {
    //Enter your code here
    const [d1,m1,y1,d2,m2,y2] = input.split(/\s/).map(Number);
    if (y1 !== y2) console.log(y1 > y2 ? 10000 : 0);
    else if(m1 !== m2) console.log(m1 > m2 ? (m1 - m2) * 500 : 0);
    else console.log(d1 > d2 ? (d1 - d2) * 15 : 0);
} 

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

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

Copy The Code & Try With Live Editor

#7 Code Example with Python Programming

Code - Python Programming


actually = list(map(int, input().split()))
da, ma, ya = actually

expected = list(map(int, input().split()))
de, me, ye = expected

fine = 0

if ya > ye:
    fine = 10000
elif ya == ye:
    if ma > me:
        fine = (ma - me) * 500
    elif ma == me and da > de:
        fine = (da - de) * 15

print(fine)
Copy The Code & Try With Live Editor

#8 Code Example with PHP Programming

Code - PHP Programming


 0 ? 15 * ($d1 - $d2) : 0;
    $m = 500 * ($m1 - $m2) > 0 ? 500 * ($m1 - $m2) : 0;
    $y = $y1 - $y2 > 0 ? 10000 : 0;

    if ($y > 0) {
        return $y;
    }

    if ($m > 0) {
        return $m;
    }

    if ($d > 0) {
        return $d + $m + $y;
    }

    return 0;
}

$_fp = fopen("php://stdin", "r");

fscanf($_fp, "%[^\n]", $date1);
fscanf($_fp, "%[^\n]", $date2);

$date1 = date_parse_from_format('j m Y', $date1);
$date2 = date_parse_from_format('j m Y', $date2);

echo libraryFine($date1['day'], $date1['month'], $date1['year'], $date2['day'], $date2['month'], $date2['year']);

?>
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Day 25: Running Time and Complexity solution in Hackerrank - Hacerrank solution C, C++, C#, GO, java, Js, Python & PHP in 30 days of code
Next
[Solved] Day 27: Testing solution in Hackerrank - Hacerrank solution C, C++, C#, GO, java, Js, Python & PHP in 30 days of code