Day 4: Normal Distribution #2

  • + 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))