Menu



Manage

Cord > Study_Algorithm 전체 다운로드
Study_Algorithm > 4/week04_03_chap03_06.py Lines 50 | 1.2 KB
다운로드

                        # week04_03_chap03_06

def print_poly(t_x, f_x):
    """
    다항식을 문자열로 출력하는 함수
    :param t_x: 정수형 지수를 원소를 가지는 리스트
    :param f_x: 정수형 계수를 원소를 가지는 리스트
    :return: 완성된 다항식 문자열
    """
    poly_str = "f(x) = "

    for i in range(len(fx)):  # 0 ~ 3, i
        coefficient = f_x[i]
        term = t_x[i]

        if coefficient >= 0:
            poly_str = poly_str + "+"
        poly_str = poly_str + f"{coefficient}x^{term} "

    return poly_str


def calc_poly(x_value, t_x, f_x):
    """
    다항식 산술 연산 함수
    :param x_value: 독립 변수, 정수형
    :param t_x: 정수형 지수를 원소를 가지는 리스트
    :param f_x: 정수형 계수를 원소를 가지는 리스트
    :return:
    """
    return_value = 0

    for i in range(len(fx)):
        term = t_x[i]
        coefficient = f_x[i]
        return_value = return_value + (coefficient * (x_value ** term))

    return return_value


tx = [30, 5, 1]
fx = [2, -3, 7]  # f(x) = +2x^30 -3x^5 +7x^1



if __name__ == "__main__":
    print(print_poly(tx, fx))

    x = int(input("X 값 : "))
    print(calc_poly(x, tx, fx))