Day 4: Normal Distribution #2

Sort by

recency

|

8 Discussions

|

  • + 0 comments
    import math
    
    mean = 20 
    std = 2
    
    def calc_prob(lower_bound, upper_bound=None):
        if upper_bound is None:
            prob = 0.5 * (1 + math.erf((lower_bound - mean) / (std * math.sqrt(2))))
        else:
            prob = 0.5 * (math.erf((upper_bound - mean) / (std * math.sqrt(2))) -
                                 math.erf((lower_bound - mean) / (std * math.sqrt(2))))  
        return prob
    
    print(round(calc_prob(19.5), 3))
    
    print(round(calc_prob(20,22), 3))
    
  • + 0 comments

    Here is my answer:

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import math
    def get_function(x):
        mu = 20
        sig = 2
        gaussian_func = (1 / (sig * math.sqrt(2 * math.pi))) * math.exp(-((x - mu) ** 2) / (2 * sig ** 2))
        return gaussian_func
    
    start_value = 12
    end_value = 28
    step = 0.0001
    
    x = [start_value + i * step for i in range(int((end_value - start_value) / step) + 1)]
    y = 0
    y1 = 0
    y2 = 0
    
    for i in range(len(x)):
        y += get_function(x[i]) * step
        if x[i] < 19.5:
            y1 += get_function(x[i]) * step
    
        if 20 < x[i] < 22:
            y2 += get_function(x[i]) * step
    
    result1 = int((y1 / y) * 1000) / 1000
    result2 = int((y2 / y) * 1000) / 1000
    
    print(str(result1))
    print(str(result2))
    
  • + 0 comments

    For anyone having conceptual trouble, check out these links:

    Fancy functions won't help you learn

  • + 0 comments

    Hackerrank should be flagged as "unstable prototype".

    2 points I noted: The answers here have to be entered in the wrong order for the "Correct answer message". R code that works on any other machine, regularly fails on hackerrank. Pretty embarrassing for a website that claims to assess your IT skills.

  • + 1 comment

    I submitted R code, it works fine in Rstudio and result is 0.401 and 0.341; but site says its incorrect. Am i missing anything?

    cat(round(pnorm(19.5,20,2),3));

    cat(round(pnorm(22,20,2),3) - round(pnorm(20,20,2),3))