hbui
2024-07-15 f736ce6b3b90d11bac59244c89f54c8528bd27d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
from typing import Iterable
from sys import stdin
 
 
def horner(coefficients: list[float], b: float) -> tuple[float, list[float]]:
    """
    evaluiert Polynom mit Koeffizienten an der stelle b
    :param coefficients:
    :param b:
    :return:
    """
    p = coefficients[-1]
    c = [p]
    for ak in coefficients[-2::-1]:
        p = ak + (p * b)
        c.insert(0, p)
    return c[0], c[1:]
 
 
def read_coefficients(input_data: Iterable):
    print(f"Input Coefficients line by line, last line is x0, Press Ctr+D to finish!")
    lines = []
    for line in input_data:
        koeffizient = line.strip()
        if koeffizient[0] == "#":
            continue
        if len(koeffizient) > 0:
            lines.append(float(koeffizient))
        else:
            break
    return (lines[0:-1],lines[-1])
 
 
if __name__ == "__main__":
    (coefficient, b) = read_coefficients(stdin)
    (pb, c) = horner(coefficient, b)
    print(pb, c)