diff --git a/img/stapeldiagramm_fakultaet.pdf b/img/stapeldiagramm_fakultaet.pdf new file mode 100644 index 0000000000000000000000000000000000000000..620f61d0632f7ccff00d2a67051a664a1cd2b9e8 Binary files /dev/null and b/img/stapeldiagramm_fakultaet.pdf differ diff --git a/img/stapeldiagramm_fakultaet.svg b/img/stapeldiagramm_fakultaet.svg new file mode 100644 index 0000000000000000000000000000000000000000..af48843ae815996fd8d68567ca6f7ce3fa2e68af --- /dev/null +++ b/img/stapeldiagramm_fakultaet.svg @@ -0,0 +1,368 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="106.10155mm" + height="46.042965mm" + viewBox="0 0 106.10155 46.042965" + version="1.1" + id="svg8" + inkscape:version="0.92.1 r15371" + sodipodi:docname="stapeldiagramm_fakultaet.svg"> + <defs + id="defs2"> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM" + style="overflow:visible" + inkscape:isstock="true"> + <path + id="path4657" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:stockid="Arrow1Send" + orient="auto" + refY="0" + refX="0" + id="Arrow1Send" + style="overflow:visible" + inkscape:isstock="true"> + <path + id="path4527" + d="M 0,0 5,-5 -12.5,0 5,5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="matrix(-0.2,0,0,-0.2,-1.2,0)" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-6" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-6" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-1" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-9" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + <marker + inkscape:stockid="TriangleOutM" + orient="auto" + refY="0" + refX="0" + id="TriangleOutM-4" + style="overflow:visible" + inkscape:isstock="true"> + <path + inkscape:connector-curvature="0" + id="path4657-8" + d="M 5.77,0 -2.88,5 V -5 Z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + transform="scale(0.4)" /> + </marker> + </defs> + <sodipodi:namedview + id="base" + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1.0" + inkscape:pageopacity="0.0" + inkscape:pageshadow="2" + inkscape:zoom="2.2123811" + inkscape:cx="174.30497" + inkscape:cy="87.010327" + inkscape:document-units="mm" + inkscape:current-layer="layer1" + showgrid="false" + inkscape:window-width="1920" + inkscape:window-height="1009" + inkscape:window-x="0" + inkscape:window-y="34" + inkscape:window-maximized="1" + fit-margin-top="0" + fit-margin-left="0" + fit-margin-right="0" + fit-margin-bottom="0" /> + <metadata + id="metadata5"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <g + inkscape:label="Ebene 1" + inkscape:groupmode="layer" + id="layer1" + transform="translate(-20.249839,-22.111829)"> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="29.623943" + y="26.790508" + id="text4487"><tspan + sodipodi:role="line" + id="tspan4485" + x="29.623943" + y="26.790508" + style="stroke-width:0.26458332px">__main__</tspan><tspan + sodipodi:role="line" + x="29.623943" + y="31.023842" + style="stroke-width:0.26458332px" + id="tspan4489" /></text> + <text + id="text4495" + y="37.232071" + x="31.004736" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + id="tspan4493" + style="stroke-width:0.26458332px" + y="37.232071" + x="31.004736" + sodipodi:role="line">fakultaet</tspan></text> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="31.004736" + y="56.531811" + id="text4501"><tspan + sodipodi:role="line" + x="31.004736" + y="56.531811" + style="stroke-width:0.26458332px" + id="tspan4499">fakultaet</tspan></text> + <g + id="g4506" + transform="translate(-0.43764877,-0.765871)"> + <rect + y="52.354485" + x="44.727676" + height="6.6522522" + width="74.327888" + id="rect4537" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="48.266571" + y="56.955494" + id="text4527"><tspan + sodipodi:role="line" + x="48.266571" + y="56.955494" + style="text-align:start;text-anchor:start;stroke-width:0.26458332px" + id="tspan4525">n → 1 rekursion → 1 ergebnis → 1 </tspan></text> + </g> + <rect + y="22.244329" + x="44.290028" + height="6.6522541" + width="74.327888" + id="rect4501" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <g + id="g4514" + transform="translate(-0.43764877,-10.547299)"> + <rect + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" + id="rect4508" + width="74.327888" + height="6.6522522" + x="44.727676" + y="52.354485" /> + <text + id="text4512" + y="56.955494" + x="48.266571" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + id="tspan4510" + style="text-align:start;text-anchor:start;stroke-width:0.26458332px" + y="56.955494" + x="48.266571" + sodipodi:role="line">n → 2 rekursion → 1 ergebnis → 2 </tspan></text> + </g> + <g + transform="translate(-0.43764877,-20.328727)" + id="g4522"> + <rect + y="52.354485" + x="44.727676" + height="6.6522522" + width="74.327888" + id="rect4516" + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="48.266571" + y="56.955494" + id="text4520"><tspan + sodipodi:role="line" + x="48.266571" + y="56.955494" + style="text-align:start;text-anchor:start;stroke-width:0.26458332px" + id="tspan4518">n → 3 rekursion → 2 ergebnis → 6 </tspan></text> + </g> + <g + id="g4530" + transform="translate(-0.43764877,9.0155572)"> + <rect + style="opacity:0.99823531;fill:#aaccff;fill-opacity:0.60784314;stroke:#0067ff;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" + id="rect4524" + width="74.327888" + height="6.6522522" + x="44.727676" + y="52.354485" /> + <text + id="text4528" + y="56.955494" + x="48.266571" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + id="tspan4526" + style="text-align:start;text-anchor:start;stroke-width:0.26458332px" + y="56.955494" + x="48.266571" + sodipodi:role="line">n → 0</tspan></text> + </g> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + x="31.004736" + y="46.881939" + id="text4534"><tspan + sodipodi:role="line" + x="31.004736" + y="46.881939" + style="stroke-width:0.26458332px" + id="tspan4532">fakultaet</tspan></text> + <text + id="text4538" + y="66.181686" + x="31.004736" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve"><tspan + id="tspan4536" + style="stroke-width:0.26458332px" + y="66.181686" + x="31.004736" + sodipodi:role="line">fakultaet</tspan></text> + <path + style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM)" + d="m 118.91762,65.266588 c 4.87542,0 5.7704,-8.37145 0.71755,-8.37145" + id="path4510" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <path + style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-6)" + d="m 119.1568,55.974267 c 4.87542,0 5.53122,-9.806556 0.47837,-9.806556" + id="path4510-2" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <path + style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-1)" + d="m 119.1568,45.246841 c 4.87542,0 5.53122,-9.806556 0.47837,-9.806556" + id="path4510-6" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <path + style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26499999;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#TriangleOutM-4)" + d="m 119.1568,34.519415 c 4.87542,0 5.53122,-9.806556 0.47837,-9.806556" + id="path4510-0" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <text + id="text4495-8" + y="62.854229" + x="125.06536" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve" + inkscape:transform-center-x="0.59796058" + inkscape:transform-center-y="-0.59796058"><tspan + id="tspan4493-1" + style="stroke-width:0.26458332px" + y="62.854229" + x="125.06536" + sodipodi:role="line">1</tspan></text> + <text + id="text4495-8-2" + y="52.93325" + x="125.06536" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve" + inkscape:transform-center-x="0.59796058" + inkscape:transform-center-y="-0.59796058"><tspan + id="tspan4493-1-9" + style="stroke-width:0.26458332px" + y="52.93325" + x="125.06536" + sodipodi:role="line">1</tspan></text> + <text + id="text4495-8-7" + y="41.332817" + x="125.39848" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve" + inkscape:transform-center-x="0.59796058" + inkscape:transform-center-y="-0.59796058"><tspan + id="tspan4493-1-5" + style="stroke-width:0.26458332px" + y="41.332817" + x="125.39848" + sodipodi:role="line">2</tspan></text> + <text + id="text4495-8-6" + y="31.287079" + x="125.36715" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:100%;font-family:arial;-inkscape-font-specification:arial;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + xml:space="preserve" + inkscape:transform-center-x="0.59796058" + inkscape:transform-center-y="-0.59796058"><tspan + id="tspan4493-1-4" + style="stroke-width:0.26458332px" + y="31.287079" + x="125.36715" + sodipodi:role="line">6</tspan></text> + </g> +</svg> diff --git a/notebooks/seminar06.ipynb b/notebooks/seminar06.ipynb index 6037a920ecc156834c50756787bf1f7856112c3e..3a221e4846aa8d435d89a64d58bfde556a397e90 100644 --- a/notebooks/seminar06.ipynb +++ b/notebooks/seminar06.ipynb @@ -61,7 +61,7 @@ "source": [ "Die (meisten) Funktionen, die wir bisher geschrieben haben sind \"leer\" - sie haben keinen Rückgabewert. Präziser ausgedrückt ist ihr Rückgabewert `None` (also nichts).\n", "\n", - "In diesem Kapitel schreiben wir (endlich) ertragreiche Funktionen. Das erste Beispiel ist die Funktion `area`, die die Fläche eines Kreises für einen gegebenen Radius berechnet:" + "In diesem Kapitel schreiben wir (endlich) ertragreiche Funktionen. Das erste Beispiel ist die Funktion `kreisflaeche`, die die Fläche eines Kreises für einen gegebenen Radius berechnet:" ] }, { @@ -70,7 +70,7 @@ "metadata": {}, "outputs": [], "source": [ - "def area(radius):\n", + "def kreisflaeche(radius):\n", " a = math.pi * radius**2\n", " return a" ] @@ -88,7 +88,7 @@ "metadata": {}, "outputs": [], "source": [ - "def area(radius):\n", + "def kreisflaeche(radius):\n", " return math.pi * radius**2" ] }, @@ -350,7 +350,302 @@ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "### 6.3 Komposition\n", + "\n", + "Wie Sie mittlerweile wissen sollten, können wir eine Funktion innerhalb einer anderen aufrufen. Als Beispiel werden wir eine Funktion schreiben, die zwei Punkte erwartet - den Mittelpunkt eines Kreises und einen Punkt auf dem Kreisumfang - und uns daraus die Fläche des Kreises berechnet.\n", + "\n", + "Angenommen, die Koordinaten des Mittelpunktes sind in den Variablen `xc` und `yc` gespeichert und die des Punktes auf dem Kreisumfang in `xp` und `yp`. Der erste Schritt ist, den Radius des Kreises zu berechnen, der sich aus der Entfernung der beiden Punkte ergibt. Wir haben gerade eine Funktion `entfernung` geschrieben, die das erledigt: " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "radius = entfernung(xc, yc, xp, yp)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der nächste Schritt ist, die Fläche eines Kreises mit diesem Radius zu berechnen. Das haben wir auch schon implementiert:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ergebnis = kreisflaeche(radius)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wenn wir diese Schritte in einer Funktion verkapseln, erhalten wir:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def kreisflaeche_2(xc, yc, xp, yp):\n", + " radius = entfernung(xc, yc, xp, yp)\n", + " ergebnis = kreisflaeche(radius)\n", + " return ergebnis" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Die Hilfsvariablen `radius` und `ergebnis` sind hilfreich für Entwicklung und Debugging, aber sobald das Programm funktioniert können wir es prägnanter aufschreiben durch die Komposition von Funktionsaufrufen:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def kreisflaeche_2(xc, yc, xp, yp):\n", + " return kreisflaeche(entfernung(xc, yc, xp, yp))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6.4 Boolesche Funktionen\n", + "\n", + "Funkionen können Boolesche Werte zurückliefern. Das ist praktkisch, um komplizierte Tests in einer Funktion zu verstecken. Zum Beispiel: " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def ist_teilbar(x, y):\n", + " if x % y == 0:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Es ist üblich, Booleschen Funktionen Namen zu geben, die wie Ja-/Nein-Fragen klingen; `ist_teilbar` gibt entweder `True` oder `False` zurück und zeigt damit an, ob `x` durch `y` teilbar ist.\n", + "\n", + "Hier ist ein Beispiel:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ist_teilbar(6, 4)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ist_teilbar(6, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Das Ergebnis des `==`-Operators ist ein Boolescher Wert, daher können wir die Funktion kompakter aufschreiben, indem wir den Wert direkt zurückgeben:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def ist_teilbar(x, y):\n", + " return x % y == 0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Boolesche Funktionen werden oft in Verzweigungen genutzt:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if ist_teilbar(x, 2):\n", + " print('x ist eine gerade Zahl')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Es mag verlockend erscheinen, stattdessen folgendes zu schreiben:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if ist_teilbar(x, 2) == True:\n", + " print('x ist eine gerade Zahl')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Aber dieser zusätzliche Vergleich ist unnötig.\n", + "\n", + "Schreiben Sie als Übung eine Funktion `ist_zwischen(x, y, z)`, die `True` zurückgibt, wenn $x \\le y \\le z$ gilt und ansonsten `False`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# def implementieren Sie hier die Funktion" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6.5 Noch mehr Rekursion\n", + "\n", + "Wir haben bisher nur eine kleine Teilmenge von Python kennengelernt aber vielleicht interessiert es Sie zu wissen, dass diese Teilmenge eine *komplette* Programmiersprache darstellt. Das heißt, alles was berechnet werden kann, können wir mit den bisher erlernten Anweisungen und Funktionen ausdrücken! Jedes jemals geschriebene Programm könnten wir umschreiben, so dass es nur mit den Sprachmerkmalen auskommt, die wir bis jetzt gelernt haben (gut, wir bräuchten noch ein paar Anweisungen um Geräte wie z.B. die Maus, Festplatten, etc. zu kontrollieren).\n", + "\n", + "\n", + "\n", + "Diese Behauptung zu beweisen ist eine nicht so ganz einfache Aufgabe, die zuerst von [Alan Turing](https://de.wikipedia.org/wiki/Alan_Turing) gelöst wurde. Er war einer der ersten Informatiker (einige würden argumentieren, dass er ein Mathematiker war, aber viele der ersten Informatiker begannen als Mathematiker). Dementsprechend wird dies oft als [Turing-These](https://de.wikipedia.org/wiki/Church-Turing-These) bezeichnet. \n", + "\n", + "Um einen Idee davon zu bekommen, was wir mit den Werkzeugen, die wir bisher kennengelernt haben, schon erreichen können, wollen wir einige rekursiv definierte mathematische Funktionen implementieren. Eine rekursive Definition ist ähnlich einer [zirkulären Definition](https://en.wikipedia.org/wiki/Circular_definition) (*circular definition* - leider konnte ich dafür keinen deutschen Begriff finden) in dem Sinne, dass die Definition eine Referenz auf das was definiert wird enthält. Eine richtig zirkuläre Definition ist nicht sehr nützlich:\n", + "\n", + "**vorpal:**\n", + "- Ein Adjektiv welches genutzt wird, um etwas zu beschreiben, was vorpal ist.\n", + "\n", + "Wenn Sie so eine Definition in einem Wörterbuch sehen, sind sie vermutlich verärgert. Andererseits, wenn wir uns die Definition der Fakultätsfunktion heraussuchen (die mit dem Symbol ! bezeichnet wird), finden wir vermutlich etwas in der Art:\n", + "\n", + "\\begin{align}\n", + "0! &= 1\\\\\n", + "n! &= n(n-1)!\n", + "\\end{align}\n", + "\n", + "Diese Definition sagt aus, dass die Fakultät von 0 gleich 1 ist und die Fakultät jedes anderen Wertes $n$ entspricht $n$ multipliziert mit der Fakultät von $n-1$.\n", + "\n", + "Also ist 3! gleich 3 mal 2!, was 2 mal 1! ist, was 1 mal 0! ist. Zusammengenommen ist 3! also gleich 3 mal 2 mal 1 mal 1 - also 6.\n", + "\n", + "Wenn wir etwas rekursiv definieren können, dann können wir auch eine Python-Funktion schreiben, um das ganze auszuwerten. Der erste Schritt ist, zu entscheiden, was die Parameter sein sollen. In diesem Beispiel sollte es klar sein, dass `fakultaet` eine ganze Zahl erwartet: \n", + "\n", + "```python\n", + "def fakultaet(n):\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wenn das Argument 0 übergeben wird, müssen wir einfach nur 1 zurückgeben:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fakultaet(n):\n", + " if n == 0:\n", + " return 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ansonsten, und das ist der spannende Teil, müssen wir einen rekursiven Aufruf machen, um die Fakultät von $n-1$ zu berechnen und dann mit $n$ zu multiplizieren:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fakultaet(n):\n", + " if n == 1:\n", + " return 1\n", + " else:\n", + " rekursion = fakultaet(n-1)\n", + " ergebnis = n * rekursion\n", + " return ergebnis\n", + "\n", + "fakultaet(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Kontrollfluss dieses Programms ist ähnlich dem von `countdown` in [Abschnitt 5.8](seminar05.ipynb#5.8-Rekursion). Wenn wir `fakultaet` mit dem Wert 3 aufrufen, passiert folgendes:\n", + "\n", + "Da 3 ungleich 0 ist, führen wir den zweiten Zweig aus und berechnen die Fakultät von $n-1$ ...\n", + "- Da 2 ungleich 0 ist, führen wir den zweiten Zweig aus und berechnen die Fakultät von $n-1$ ...\n", + " - Da 1 ungleich 0 ist, führen wir den zweiten Zweig aus und berechnen die Fakultät von $n-1$ ...\n", + " - Da 0 gleich 0 ist, führen wir den ersten Zweig aus und geben 1 zurück, ohne weitere rekursive Aufrufe zu tätigen.\n", + " \n", + " Der Rückgabewert, 1, wird mit n multipliziert, was 1 ist, und das Ergebnis zurückgegeben.\n", + " \n", + " Der Rückgabewert, 1, wird mit n multipliziert, was 2 ist, und das Ergebnis zurückgegeben.\n", + " \n", + "Der Rückgabewert, 2, wird mit n multipliziert, was 3 ist, und das Ergebnis 6 wird zum Rückgabewert des Funktionsaufrufs, der den ganzen Vorgang gestartet hat.\n", + "\n", + "Die folgende Abbildung zeigt wie das Stapeldiagramm für diese Folge von Funktionsaufrufen aussieht:\n", + "\n", + "\n", + "\n", + "Das Diagramm zeigt, wie die Rückgabewerte im Stapel weiter nach oben durchgereicht werden. In jedem Block ist der Rückgabewert der Wert von `ergebnis`, was das Produkt von `n` und `rekursion` ist.\n", + "\n", + "Im letzten Block" + ] }, { "cell_type": "markdown",