Nested logic problem solution | 30 days of code | HackerRank

Input Format
The first line contains  space-separated integers denoting the respective , and  on which the book was actually returned.
The second line contains  space-separated integers denoting the respective , and  on which the book was expected to be returned (due date).
Constraints

Output Format
Print a single integer denoting the library fine for the book received as input.
Nested logic problem solution | 30 days of code | HackerRank

Problem solution in Bash programming language

read d_act m_act y_act
read d_exp m_exp y_exp

if (( y_act > y_exp )); then
    echo 10000
elif (( y_act == y_exp )); then
    if (( m_act > m_exp )); then
        echo $(( 500 * (m_act - m_exp) ))
    elif (( m_act == m_exp )); then
        if (( d_act > d_exp )); then
            echo $(( 15 * (d_act - d_exp) ))
        else
            echo 0
        fi
    else
        echo 0
    fi
else
    echo 0
fi

Problem solution in c++ programming language.



#include <iostream>
using namespace std;

int main(int argc, const char* args[]){
    int actualDay;
    int actualMonth;
    int actualYear;
    int expectedDay;
    int expectedMonth;
    int expectedYear;

    // Actual return date
    cin >> actualDay;   
    cin >> actualMonth; 
    cin >> actualYear;  

    // Expected return date
    cin >> expectedDay;
    cin >> expectedMonth;
    cin >> expectedYear;

    int fine = 0;

    // Returned within the current year:
    if(expectedYear == actualYear){
        if(expectedMonth < actualMonth){
            // Returned 1+ months late in current year
            fine = (actualMonth - expectedMonth) * 500;
        }
        else if( (expectedMonth == actualMonth) 
                && (expectedDay < actualDay) ){
            // Returned 1+ days late within the current month
            fine = (actualDay - expectedDay) * 15;
        }
        // Else it is implied that the book was returned early
    }
    else if(expectedYear < actualYear){
        // Returned 1+ years late
        fine = 10000;
    }
    // Else it is implied that the book was returned 1+ years early

    cout << fine << endl;
}

Problem solution in Java programming language.

import java.util.*;

class BookReturnDate{
    public int day;
    public int month; 
    public int year;
    
    BookReturnDate(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }
}

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        BookReturnDate actual = new BookReturnDate(scan.nextInt(), scan.nextInt(),scan.nextInt());
        BookReturnDate expected = new BookReturnDate(scan.nextInt(), scan.nextInt(),scan.nextInt());
        scan.close();
        
        int fine = 0;
        
        // Returned within the current year:
        if(expected.year == actual.year){
            // Returned 1+ months late in current year
            if(expected.month < actual.month){
                fine = (actual.month - expected.month) * 500;
            }
            // Returned 1+ days late within the current month
            else if( (expected.month == actual.month) 
                    && (expected.day < actual.day) ){
                fine = (actual.day - expected.day) * 15;
            }
            // Else it is implied that the book was returned early
        }
        else if(expected.year < actual.year){
            // Returned 1+ years late
            fine = 10000;
        }
        // Else it is implied that the book was returned 1+ years early

        System.out.println(fine);
        
    }
}



Problem solution in Python programming language.

n = input()
x = list(map(int, n.split()))
m = input()
y = list(map(int, m.split()))
z=0
if y[2] < x[2]:
    z = 10000
elif y[2] == x[2]:
    if y[1] < x[1]:
        z = 500*(x[1]-y[1])
    elif y[0] < x[0]:
        z = 15*(x[0]-y[0])

print(z)

Post a Comment

0 Comments