Пример #1
0
def emi_calc(principle: "Money borrowed" = 100000,
             interest: "interest rate per year" = 10,
             years: "Term of the loan" = 3):
    '''The function will returm the Equated Monthly installment amount
    for the borrowed principle over a certain period at a certain rate of interest'''
    for i in (principle, years):
        if not is_int(i):
            raise TypeError(
                f'Unsupported format,{type(i)} in the place of "int"')
    if not is_float(interest) and not is_int(interest):
        raise TypeError(
            f'Unsupported format,{type(interest)} in the place of "int" or "float"'
        )
    for i in (principle, interest, years):
        if i < 1:
            raise ValueError("Invalid Entry")

    monthly_int = interest / (12 * 100)

    months = years * 12

    present_value = 1 / (1 + monthly_int)

    v_n = present_value**months

    annuity = (1 - v_n) / monthly_int

    return round(principle / annuity)
Пример #2
0
def is_pythagorean_triple(triple):
    '''This function returns True is the passed element is a pythagorean triple,
    returns False otherwise'''
    if isinstance(triple, tuple) and len(triple) == 3:
        if is_int(triple[0]) and is_int(triple[1]) and is_int(triple[2]):
            numbers = list(triple)
            numbers.sort()
            return (numbers[0]**2 + numbers[1]**2) == numbers[2]**2

        raise TypeError("Unsupported Type")

    raise TypeError(f"Unsupported Type {type(triple)}")
Пример #3
0
def compound_interest(principle=1000, years=2, rate=7):
    '''This function will return the interest part of the principle
    compounded throughout the term.
    Note: The interest is compounded annunaly, so the year must not be a float'''
    if not is_int(years):
        raise TypeError(
            f'Unsupported Format,{type(years)} passed in the placce of integer'
        )
    for i in (principle, rate):
        if not is_float(i) and not is_int(i) or isinstance(i, bool):
            raise TypeError('Unsupported Format')
    for i in (principle, years, rate):
        if i <= 0:
            raise ValueError('invalid entry')
    return round(((principle * (1 + (rate / 100))**years) - principle), 2)
Пример #4
0
def quadratic_equation(a_x, b_y, c_c):
    '''
    This function finds the roots X and Y for the given input to a quadratic
    equation
    '''
    for i in (a_x, b_y, c_c):
        if not is_int(i) and not is_float(i):
            raise TypeError("unsupported format")
    discriminant = (b_y**2) - (4 * a_x * c_c)
    if discriminant > 0:
        root_x = ((-b_y + (discriminant)**0.5) / (2 * a_x))
        root_y = ((-b_y - (discriminant)**0.5) / (2 * a_x))
        print("The roots are real and distinct")
        print(
            f'The roots are {str(Fraction(root_x))} and {str(Fraction(root_y))}'
        )
        return root_x, root_y
    if discriminant == 0:
        root_x = root_y = -b_y / (2 * a_x)
        print("The roots are real and equal")
        print(
            f'The roots are {str(Fraction(root_x))} and {str(Fraction(root_y))}'
        )
        return root_x, root_y
    if discriminant < 0:
        root_x = root_y = -b_y / (2 * a_x)
        imaginary = ((discriminant)**0.5) / (2 * a_x)
        root_x_1 = ComplexCustom(root_x + imaginary)
        root_x_2 = ComplexCustom(root_x - imaginary)
        print("The roots are complex")
        print(f'The roots are{root_x_1}and{root_x_2}')
        return root_x_1, root_x_2
    return None
def sort_complex_numbers(*complex_numbers):
    '''The function returns a sorted tuple of passed complex numbers
    based on the real part of those complex numbers'''
    for i in complex_numbers:
        if not is_int(i) and not is_float(i) and not isinstance(i, complex):
            raise TypeError("Unsupported format")
    list_complex = list(complex_numbers)
    list_complex.sort(key=lambda complex_num: complex_num.real)
    return tuple(list_complex)
Пример #6
0
def number_to_list_digits(number):
    '''This function returns individual digits'''
    if not is_int(number):
        raise ValueError(f"Invalid Inputs {type(number)}, {number}")

    number_str = str(number)
    if number_str.isnumeric():
        return [int(char) for char in number_str]
    raise ValueError(f"Invalid Inputs {type(number)}, {number}")
Пример #7
0
def calc_int_paid(amt_borrowed: "Money borrowed" = 100000,
                  int_rate: "interest rate per year" = 10,
                  term: "Term of the loan" = 3):
    '''The function will calculate the emi for the loan and then returns the interest amount
    paid during the term of the loan'''
    for i in (amt_borrowed, term):
        if not is_int(i):
            raise TypeError(
                f'Unsupported format,{type(i)} in the place of "int"')
    if not is_float(int_rate) and not is_int(int_rate):
        raise TypeError(
            f'Unsupported format,{type(int_rate)} in the place of "int" or "float"'
        )
    for i in (amt_borrowed, int_rate, term):
        if i < 1:
            raise ValueError("Invalid Entry")

    #Encapsulating of the function to calculate emi
    def calc_emi(amt_borrowed, int_rate, term):
        monthly_int = int_rate / (12 * 100
                                  )  #converting the yearly interest to monthly
        months = term * 12
        present_value = 1 / (1 + monthly_int
                             )  #Finding out present value for Re.1
        v_n = present_value**months
        annuity = (1 - v_n) / monthly_int  #annuity for the term
        return amt_borrowed / annuity

    #The below method is to calculate the total interest from the loan schedule

    emi = calc_emi(amt_borrowed, int_rate, term)
    interest_amt = amt_borrowed * (int_rate / (12 * 100))
    total_interest = interest_amt
    principle_amt = emi - interest_amt
    loan_outstanding = amt_borrowed - principle_amt

    while loan_outstanding > 0:
        interest_amt = loan_outstanding * (int_rate / (12 * 100))
        total_interest += interest_amt
        principle_amt = emi - interest_amt
        loan_outstanding = loan_outstanding - principle_amt

    return round(total_interest)
Пример #8
0
def is_prime(number):
    '''This fucntion returns True if the number is prime,
    False otherwise'''
    if not is_int(number):
        raise TypeError(f"Unsupported Type {type(number)}")

    for divisor in range(2, number):
        if number % divisor == 0:
            return False

    return True
def product_prime(number):
    '''This function returns the product of the numbers which are prime factors
    of a given number'''
    if not is_int(number):
        raise TypeError(f'unsupported format, {type(number)} is in the place of"integer"')
    if number <= 0:
        raise ValueError("pass an integer greater than 0")
    product = 1
    for i in range(2, number+1):
        if number % i == 0 and is_prime(i):
            product = product * i
    return product
    
Пример #10
0
def prime_before(number):
    '''The Function will return the prime number that comes first
    when we count from backwards of the given number
    Note: The number passed should be greater than 2'''
    if not is_int(number):
        raise TypeError("Unsupported format")
    if number <= 2:
        raise ValueError('Invalid input')
    for i in range(number - 1, 1, -1):
        if is_prime(i):
            return i
    #This return will never be executed
    return None
Пример #11
0
def factorial_num(number):
    '''The function will return the factorial of the given number'''
    if not is_int(number):
        raise TypeError(
            f'unsupported format, {type(number)} is in the place of"integer"')
    if number < 0:
        raise ValueError("pass a positive integer")
    if number in (0, 1):
        return 1
    result = 1
    if number > 1:
        result = number * factorial_num(number - 1)
    return result
Пример #12
0
def area(length, *b):
    '''This function will take passed arguments and return
    area of square if b is not given, else it will
    return the area of rectangle'''
    if not (is_int(length) or is_float(length)):
        raise TypeError(
            f"Unsupported type,'{type(length)}' in the place of 'int' or 'float'"
        )
    if length <= 0:
        raise ValueError("Invalid entry")
    if b:
        if len(b) > 1:
            raise ValueError("unpacking cannot be done")
        if not (is_int(b[0]) or is_float(b[0])):
            raise TypeError(
                f"Unsupported type,'{type(b[0])}' in the place of 'int' or 'float'"
            )
        if b[0] <= 0:
            raise ValueError("Invalid entry")
        breadth, = b
        if length != breadth:
            return length * breadth
    return length * length
Пример #13
0
def is_armstrong_number(number):
    '''This function returns True if the number is amstrong number,
    returns False otherwise'''
    if not is_int(number):
        raise TypeError(f"Unsupported type {type(number)}")

    num_str = str(number)
    num_digits = len(num_str)
    sum_of_digits = 0

    for digit in num_str:
        sum_of_digits += (int(digit)**num_digits)

    return number == sum_of_digits
Пример #14
0
def is_abundant_number(number):
    '''The above function will return True if the given number is an abundant number or not.
    Abundant Number: if the sum of the divisors(excluding the given number) of given number,
    exceeds the given number then it is said to be abundant
    Note: all prime numbers are not abundant.'''

    if not is_int(number):
        raise TypeError("unsupported format, Enter an Integer")

    sum_divisors = 1
    for i in range(2, number):
        if number % i == 0:
            sum_divisors += i

    return sum_divisors > number
Пример #15
0
def sep_odd_eve(list_int):
    '''when a list containing set of integers is passed, this function returns
    two tuple within a tuple one containing odd integers and the other containing even integers'''
    if not isinstance(list_int, list):
        raise TypeError("unsupported format, pass a list")
    for i in list_int:
        if not is_int(i):
            raise TypeError(
                "unsupported format, pass integers inside the list")
    odd_list = []
    even_list = []
    for i in list_int:
        if i % 2 != 0:
            odd_list.append(i)
        else:
            even_list.append(i)
    return tuple(odd_list), tuple(even_list)
Пример #16
0
def derivative_x_pow(dif, num):
    '''The function will return the nth derivative of "x" raised to the power of the given number'''
    for i in (dif, num):
        if not is_int(i):
            raise TypeError("unsupported format")
    if dif <= 0:
        raise ValueError(
            "invalid entry, enter a valid number to differentiate")
    if (dif > num) and num in (0, 1):
        return 0
    if dif >= num > 0:
        return 1
    fore = 1
    index = num
    for _ in range(1, dif + 1):
        fore = fore * index
        index = index - 1
    return f'{fore}(x^{index})'
Пример #17
0
def amicable_numbers(num1, num2):
    '''This function returns True if the numbers are amicable number,
    returns False otherwise'''
    for i in (num1, num2):
        if not is_int(i):
            raise TypeError("unsupported format")
    for i in (num1, num2):
        if i < 1:
            raise ValueError("Enter a number greater than 1")
    sum1 = 0
    sum2 = 0
    for i in range(1, num1):
        if num1 % i == 0:
            sum1 += i
    for j in range(1, num2):
        if num2 % j == 0:
            sum2 += j
    return sum1 == num2 and sum2 == num1
Пример #18
0
def test_is_int():
    assert is_int(10) == True
    assert is_int(10.1) == False
    assert is_int("10") == False
    assert is_int([1, 2, 3]) == False
    assert is_int((1, 2, 3)) == False
    assert is_int({1: 2, 2: 2}) == False
    assert is_int(True) == False
    assert is_int(None) == False
    assert is_int(set()) == False
    assert is_int(1000) == True
    assert is_int(10000000) == True
    assert is_int(1)