#!/usr/bin/env python3

import  sys
import  math

class   BadArgumentError(Exception):
    def __init__(self, message, errors = "BadArgumentError"):
        super().__init__(message)
        self.errors = errors

class   IQ():
    def __init__(self, average, standard_deviation, minimum = 0, maximum = 201):
        if average < 0 or standard_deviation < 0 or minimum < 0 or maximum < 0 or maximum > 201 or average > 200:
            raise BadArgumentError("only positives arguments")
        self.standard_deviation = standard_deviation
        self.average = average
        self.minimum = minimum
        self.maximum = maximum
        self.tab = []
        for i in range(self.minimum, self.maximum):
            self.tab.append(getGaussean(self.average, self.standard_deviation, i))

def     man_help():
    print("USAGE")
    print("    ./205IQ u s [IQ1] [IQ2]\n")
    print("DESCRIPTION")
    print("    u       mean")
    print("    s       standard deviation")
    print("    IQ1     minimum IQ")
    print("    IQ2     maximum IQ")

def     getGaussean(average, standard_deviation, x):
    return (math.exp(-math.pow(x - average, 2) / (2 * pow(standard_deviation, 2))) / (standard_deviation * math.sqrt(2 * math.pi)))

def     basicResult(iq):
    for i in range(len(iq.tab)):
        print(i, "%.5f" % iq.tab[i])

def     inferiorResult(iq):
    i = 0
    result = 0
    while i < iq.minimum:
        result += getGaussean(iq.average, iq.standard_deviation, i)
        i += 0.01
    print("%.1f%% of people have an IQ inferior to %d" % (result, iq.minimum))

def     intervalResult(iq):
    i = iq.minimum
    result = 0
    while i < iq.maximum:
        result += getGaussean(iq.average, iq.standard_deviation, i)
        i += 0.01
    print("%.1f%% of people have an IQ between %d and %d" % (result, iq.minimum, iq.maximum))

def     main():
    try:
        if len(sys.argv) == 2:
            if sys.argv[1] == "-h":
                man_help()
            else:
                raise BadArgumentError("./205IQ: Invalid argument")
        elif len(sys.argv) == 3:
            iq = IQ(int(sys.argv[1]), int(sys.argv[2]))
            basicResult(iq)
        elif len(sys.argv) == 4:
            iq = IQ(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]))
            inferiorResult(iq)
        elif len(sys.argv) == 5:
            iq = IQ(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))
            intervalResult(iq)
        else:
            raise BadArgumentError("./205IQ: Invalid number of parameters")
    except BaseException as error:
        sys.stdout.write(str(type(error).__name__) + ": {}\n".format(error))
        sys.exit(84)

if __name__ == "__main__":
    main()
