c97d23edee3e224f1bcba0add74b61934ad69ff7..01eda97ff64ad2570b7105cc5b734f488ce0d47a
4 days ago Hong-Phuc Bui
Implement plus operator
01eda9 diff | tree
4 days ago Hong-Phuc Bui
implement polynomial
4c5ba0 diff | tree
3 files modified
2 files added
156 ■■■■ changed files
.gitignore 2 ●●●●● patch | view | raw | blame | history
04-schleife/blumen.py 23 ●●●●● patch | view | raw | blame | history
directory-struct/autodir.py 40 ●●●●● patch | view | raw | blame | history
polymonial/polynomial.py 54 ●●●● patch | view | raw | blame | history
polymonial/polynomial_test.py 37 ●●●● patch | view | raw | blame | history
.gitignore
@@ -1,3 +1,5 @@
.idea/
__pycache__/
.coverage
htmlcov/
04-schleife/blumen.py
New file
@@ -0,0 +1,23 @@
import turtle
fenster_breite = 800
fenster_hoehe = 800
screen = turtle.Screen()
screen.setup(fenster_breite, fenster_hoehe)
anna = turtle.Turtle()
b = 100
h = 100
pental = 5
winkel = 360/pental
for i in range(0,pental):
    anna.circle(b)
    anna.left(winkel)
turtle.done()
directory-struct/autodir.py
New file
@@ -0,0 +1,40 @@
tree = {
    "document" : {
        "protokol" : "template.xlce",
        "tech": {
        }
    },
    "Zeichnung" : None,
    "video": None
}
from pathlib import Path
import sys
tree = [
    "document/protokoll/",
    "document/tech",
    "Zeichnung/",
    "video"
]
def mkdir_struct(tree, prefix="./"):
    parent = Path(prefix)
    if parent.is_dir():
        for p in tree:
            directory = parent.joinpath(p)
            directory.mkdir(parents=True, exist_ok=True)
    else:
        print(f"{prefix} ist kein Ordner oder existiert nicht")
prefix = sys.argv[1]
mkdir_struct(tree, prefix)
tree2 = [
    "photos/urlaub",
    "photos/screenshots",
    "bluetooth/"
]
mkdir_struct(tree2, "")
polymonial/polynomial.py
@@ -1,17 +1,11 @@
class Polynomial:
    def __init__(self, *argv: float):
        self.__coefficients = []
        for c in argv:
            self.__coefficients.append(c)
    def __repr__(self) -> str:
        coe = []
        for c in self.__coefficients:
            coe.append(f'{c}')
        return ' '.join(coe)
    def __str__(self):
        return self.__repr__()
        if len(argv) < 1:
            raise ValueError("A Polynomial has at least one coefficient.")
        if argv[-1] == 0:
            raise ValueError("The coefficient at the highest degree must not be zero.")
        self.__coefficients = tuple(argv)
        self.__order = len(argv)
    def  evaluate(self, x: float) -> tuple[float, list[float]]:
        p = self.__coefficients[-1]
@@ -19,4 +13,38 @@
        for ak in self.__coefficients[-2::-1]:
            p = ak + (p * x)
            c.insert(0, p)
        return c[0], c[1:]
        return c[0], c[1:]
    def order(self):
        return self.__order
    def __add__(self, other: "Polynomial") -> "Polynomial":
        if not isinstance(other, Polynomial):
            raise ValueError("Operator + is only applicable for Polynomial.")
        coefficients = []
        if self.__order > other.__order:
            low_order = other.__order
            heigh_order = self.__order
            higher_coefficients = self.__coefficients
        else:
            low_order = self.__order
            heigh_order = other.__order
            higher_coefficients = other.__coefficients
        for i in range(0, low_order):
            coefficients.append(self.__coefficients[i] + other.__coefficients[i])
        for j in range(i+1, heigh_order):
            coefficients.append(higher_coefficients[j])
        return Polynomial(*coefficients)
    def __getitem__(self, key):
        return self.__coefficients[key]
    def __repr__(self) -> str:
        coe = []
        for c in self.__coefficients:
            coe.append(f'{c}')
        return ' '.join(coe)
polymonial/polynomial_test.py
@@ -3,8 +3,10 @@
def test_polynomial_representation():
    c = Polynomial(5, 6, 0, -7)
    s = f'{c}'
    assert s == '5 6 0 -7'
    r = repr(c)
    s = str(c)
    print(r, s, c)
def test_evaluate():
    l = Polynomial(-12.5, 3.6)
@@ -12,17 +14,38 @@
    (y, rest) = l.evaluate(x)
    assert abs(y + 12.5) < EPSILON
def test_evaluate_2():
    l = Polynomial(11, 7, -5, -4, 2)
    x = 2
    rest_expected = [-3, -5, 0, 2]
    q_expected = [-3, -5, 0, 2]
    y_expected = 5
    (y, rest) = l.evaluate(x)
    (y, q) = l.evaluate(x)
    assert abs(y - y_expected) < EPSILON
    for (i, r) in enumerate( rest_expected ):
        assert abs(r - rest[i]) < EPSILON
    for (i, r) in enumerate( q_expected ):
        assert abs(r - q[i]) < EPSILON
def test_polynomial_add():
    p = Polynomial(-3, 4.5, 6)      # ~$p(x) = -3 + 4.5x + 6x^2$~
    q = Polynomial( 1, 0,   3, -4)  # ~$q(x) = 1 + 3x^2 - 4x^3$~
    s = p + q                       # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~
    expected = [-2, 4.5, 9, -4]
    for (i, ec) in enumerate( expected ):
        pc = s[i]
        assert abs(pc - ec) < EPSILON
def test_polynomial_add_2():
    p = Polynomial(1, 0, 3, -4)  # ~$q(x) = 1 + 3x^2 - 4x^3$~
    q = Polynomial(-3, 4.5, 6)      # ~$p(x) = -3 + 4.5x + 6x^2$~
    s = p + q                       # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~
    expected = [-2, 4.5, 9, -4]
    for (i, ec) in enumerate( expected ):
        pc = s[i]
        assert abs(pc - ec) < EPSILON
if __name__ == "__main__":
    test_evaluate_2()
    #test_evaluate_2()
    test_polynomial_add()