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