Header Ad

HackerRank Day 26 Nested Logic 30 days of code solution

In this HackerRank Day 26 Nested Logic 30 days of code problem set, we need to develop a program that can accept a line of integers separated with space. and now we need to print the day month and year using these integers on the output screen.

HackerRank Day 26 Nested Logic 30 days of code solution


Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
[da, ma, ya] = [int(x) for x in raw_input().strip().split(" ")]
[de, me, ye] = [int(x) for x in raw_input().strip().split(" ")]
if ya > ye:
    print 10000
elif ma > me:
    print 500 * (ma - me)
elif da > de:
    print 15 * (da - de)
else:
    print 0


Problem solution in Python 3 programming.

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)


Problem solution in java programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        int fine;
        
        Scanner scan = new Scanner(System.in);
        
        int returnDay = scan.nextInt();
        int returnMonth = scan.nextInt();
        int returnYear = scan.nextInt();
        
        int dueDay = scan.nextInt();
        int dueMonth = scan.nextInt();
        int dueYear = scan.nextInt();
        
        scan.close();
        
        if(returnYear <= dueYear){
            if(returnMonth <= dueMonth){
                if(returnDay <= dueDay){
                    fine = 0;
                }else{
                    fine = (returnDay - dueDay) * 15;
                }
            }else{
                fine = (returnMonth - dueMonth) * 500;
            }
        }else{
            fine = 10000;
        }
        
        System.out.println(fine);
    }
}


Problem solution in c++ programming.

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int d1;
    int m1;
    int y1;
    cin >> d1 >> m1 >> y1;
    int d2;
    int m2;
    int y2;
    cin >> d2 >> m2 >> y2;
    
    if(y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 <= d2)){
        cout << 0 << endl;
    }
    else if(m1 == m2 && y1 == y2){
        cout << 15*(d1 - d2) << endl;
    }
    else if(y1 == y2){
        cout << 500*(m1 - m2) << endl;
    }
    else {
        cout << 10000*(y1 - y2) << endl;
    }

    return 0;
}


Problem solution in c programming.

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

int main() {
    int returned[3] = {0};
    int expected[3] = {0};
    
    scanf("%d %d %d\n", &returned[0], &returned[1], &returned[2]);
    scanf("%d %d %d\n", &expected[0], &expected[1], &expected[2]);
    
    if(returned[2] > expected[2]) {
        printf("10000\n");
    } else if (returned[1] > expected[1]) {
        int months_late = returned[1] - expected[1];
        printf("%d\n", months_late * 500);
    } else if (returned[0] > expected[0]) {
        int days_late = returned[0] - expected[0];
        printf("%d\n", days_late * 15);
    } else {
        printf("0\n");
    }
    
    return 0;
}


Problem solution in Javascript programming.

function processData(input) {
    //Enter your code here
    var inputArr = input.split('\n'),
        actual = inputArr[0].split(' '),
        expected = inputArr[1].split(' '),
        month = 1,
        day = 0,
        year = 2,
        fine = 0;
        
    if (actual[year] > expected[year]) {
        fine = 10000;
    } else if (actual[month] > expected[month]) {
        fine = 500 * (actual[month] - expected[month]);
    } else if (actual[day] > expected[day]) {
        fine = 15 * (actual[day] - expected[day]);
    }
    
    console.log(fine);
} 

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

6 Comments

  1. sorry dude.. this coding (in java programming) fails in 2 test cases ..

    ReplyDelete
  2. on ts:

    const [d1, m1, y1] = readLine().split(' ').map(n => Number(n));
    const [d2, m2, y2] = readLine().split(' ').map(n => Number(n));
    let fine = 0;

    if(y1 > y2) {
    fine = 10000;
    } else if(y1 >= y2 && m1 > m2) {
    fine = 500 * (m1 - m2);
    } else if(y1 >= y2 && d1 > d2) {
    fine = 15 * (d1 - d2);
    }

    console.log(fine);

    ReplyDelete
  3. const [returned_date, due_date] = input.split('\n')
    const [returned_day, returned_month, returned_year] = returned_date.split(' ').map(n => Number(n))
    const [due_day, due_month, due_year] = due_date.split(' ').map(n => Number(n))

    let fine = 0

    if(returned_year > due_year){
    fine = 10000
    }
    else if(returned_month > due_month && returned_year == due_year){
    fine = 500 * (returned_month - due_month)
    }
    else if(returned_day > due_day && returned_month == due_month && returned_year == due_year){
    fine = 15 * (returned_day - due_day)
    }
    console.log(fine)

    ReplyDelete
  4. With Python3 code we would fail the following test:

    30 3 2015 day = 30, month = 3, year = 2015 (date returned)
    20 5 2015 day = 20, month = 5, year = 2015 (date due)

    It would return 150 (10 days difference between 20 and 30), but it should return 0.
    The issue is because there is no check that months are equal y[1] == x[1] before comparing days y[0] < x[0].

    The correct code should be the following:

    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[1] == x[1]:
    if y[0] < x[0]:
    z = 15*(x[0]-y[0])

    print(z)

    print(z)

    ReplyDelete