In this Hackerrank Day 5: Normal Distribution I 10 Days of Statistics problem In a certain plant, the time taken to assemble a car is a random variable, X, having a normal distribution with a mean of 20 hours and a standard deviation of 2 hours. What is the probability that a car can be assembled at this plant in:

  1. Less than 19.5 hours?
  2. Between 20 and 22 hours?

HackerRank Day 5: Normal Distribution I | 10 Days of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import math
mean=20.0
stddev=2.0
h1=19.5
h21,h22=20.0,22.0

def integrate(func,b,n=10000):
    step=1/n
    if(b<0):step=-step
    n=int(abs(b)*n)
    a=0.0
    sq=0.0
    for _ in range(0,n):
        sq+=step*func(a)
        a+=step
    return abs(sq)

erf = lambda b : (2*math.pi**(-0.5)) * integrate(lambda x: math.exp(-x**2),b)
phi = lambda b : (1 + erf( (b-mean) / (stddev * 2**0.5) ))/2
lesh1 = phi(0.0) - phi(h1)
beth1h2 = phi(h22) - phi(h21)
print(f'{lesh1:.3f}')
print(f'{beth1h2:.3f}')



Problem solution in Java Programming.

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

public class Solution {

   public static void main(String[] args) {
        double mean = 20;
        double sdev = 2;

        System.out.printf("%.3f\n", normal(19.5, mean, sdev));
        System.out.printf("%.3f\n", normal(22, mean, sdev) - normal(20, mean, sdev));

    }
    private static double normal(double x, double mean, double sdev) {
        return .5*(1 + erf((x - mean) / (sdev * Math.sqrt(2))));
    }
    
    public static double erf(double z) {
        double t = 1.0 / (1.0 + 0.5 * Math.abs(z));

        // use Horner's method
        double ans = 1 - t * Math.exp( -z*z   -   1.26551223 +
                                            t * ( 1.00002368 +
                                            t * ( 0.37409196 + 
                                            t * ( 0.09678418 + 
                                            t * (-0.18628806 + 
                                            t * ( 0.27886807 + 
                                            t * (-1.13520398 + 
                                            t * ( 1.48851587 + 
                                            t * (-0.82215223 + 
                                            t * ( 0.17087277))))))))));
        if (z >= 0) return  ans;
        else        return -ans;
}
}


Problem solution in C++ programming.

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

double cud(double x, double u, double o){
    return 0.5*(1+erf((x-u)/(o*pow(2,0.5))));
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    double u, o, first, second, third;
    cin >> u >> o >> first >> second >> third;
    cout << fixed << setprecision(3) << cud(first, u, o) - cud(0, u, o) << endl;
    cout << fixed << setprecision(3) << cud(third, u, o)- cud(second, u, o) << endl;
    return 0;
}


Problem solution in C programming.

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

// cumulative density (distribtuion function) for a normal;
double pnorm(double m, double sd, double a){
    double b = erf((a-m)/(sd*pow(2.0,0.5)));
    return 0.5*(1+b);
}

int main() {

    double mux, sdx, q1, lb, ub;
    scanf("%lf %lf\n %lf\n%lf %lf", &mux, &sdx, &q1, &lb, &ub);
    
    // probability less than 19.5
    
    printf("%.3lf\n", pnorm(mux, sdx, q1));
    
    // prob between 20 to 22
    
     printf("%.3lf\n", pnorm(mux, sdx, ub) - pnorm(mux, sdx, lb));
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    const processInput = input.split('\n');
    const data = processInput[0].split(' ').map(a => parseInt(a));
    const q1 = parseFloat(processInput[1]);
    const q2Process = processInput[2].split(' ');
    const q2A = parseFloat(q2Process[0]);
    const q2B = parseFloat(q2Process[1]);
    const mean = data[0];
    const std = data[1];
    
    console.log(cumulative(mean, std, q1).toFixed(3));
    console.log((cumulative(mean, std, q2B) - cumulative(mean, std, q2A)).toFixed(3));
}

function cumulative(mean, std, x) {
    let parameter = (x - mean) / (std * Math.sqrt(2));
    return (0.5) * (1 + erf(parameter));
}
        
function erf(z) {
    let t = 1.0 / (1.0 + 0.5 * Math.abs(z));

    let ans = 1 - t * Math.exp( -z*z   -   1.26551223 +
                                        t * ( 1.00002368 +
                                        t * ( 0.37409196 + 
                                        t * ( 0.09678418 + 
                                        t * (-0.18628806 + 
                                        t * ( 0.27886807 + 
                                        t * (-1.13520398 + 
                                        t * ( 1.48851587 + 
                                        t * (-0.82215223 + 
                                        t * ( 0.17087277))))))))));
    if (z >= 0) return  ans;
    else        return -ans;
}

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

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