Added Lecture 4

This commit is contained in:
DerGrumpf 2024-11-15 13:26:03 +01:00
parent 714cb93def
commit 796653da22
9 changed files with 13309 additions and 3132 deletions

View File

@ -257,17 +257,17 @@
},
"active": "91b08793b1132c55",
"lastOpenFiles": [
"Material/env/lib/python3.12/site-packages/__pycache__/pylab.cpython-312.pyc",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/WHEEL",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/REQUESTED",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/RECORD",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/METADATA",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/LICENSE",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info/INSTALLER",
"Material/env/lib/python3.12/site-packages/matplotlib-3.9.2.dist-info",
"Material/env/lib/python3.12/site-packages/mpl_toolkits/mplot3d/tests/__pycache__/test_legend3d.cpython-312.pyc",
"Material/env/lib/python3.12/site-packages/mpl_toolkits/mplot3d/tests/__pycache__/test_axes3d.cpython-312.pyc",
"Material/env/lib/python3.12/site-packages/mpl_toolkits/mplot3d/tests/__pycache__/test_art3d.cpython-312.pyc",
"Material/3.Lösungen_Extended_Applications.slides.html",
"Material/wise_24_25/Folien/3.Lösungen_Extended_Applications.ipynb",
"Material/wise_24_25/Folien/Untitled.ipynb",
"Material/wise_24_25/Folien",
"Material/wise_24_25/lernmaterial/4.NumPy_MatPlotLib.ipynb",
"Material/wise_24_25/lernmaterial/3.Extended_Applications.ipynb",
"Material/wise_24_25/lernmaterial/2.Tutorial.ipynb",
"Material/wise_24_25/lernmaterial/1.Tutorial.ipynb",
"Material/wise_24_25/3.Extended_Applications.ipynb",
"Material/wise_24_25/2.Tutorial.ipynb",
"Material/wise_24_25/1.Tutorial.ipynb",
"Material/env/lib/python3.12/site-packages/matplotlib/mpl-data/sample_data/logo2.png",
"Material/env/lib/python3.12/site-packages/matplotlib/mpl-data/sample_data/grace_hopper.jpg",
"Material/env/lib/python3.12/site-packages/matplotlib/mpl-data/sample_data/Minduka_Present_Blue_Pack.png",

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,334 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c3c41172-0fa4-4542-af74-5912b25dce09",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"# Lösungen Extended Applications"
]
},
{
"cell_type": "markdown",
"id": "0200f54c-1416-4e4b-bcb9-fbe781bff590",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"### Aufgabe\n",
"\n",
"*3 Punkte*\n",
"\n",
"Schreibe einen Generator `square_count` mit einem Eingabeparameter `n`, welcher die Quadratzahlen von $1\\dots n^2$ ausgibt.\n",
"\n",
"Die Aufgabe gibt 0 Punkte, wenn die Funktion `square_count` kein Generator ist!\n",
"\n",
"Hinweis: Bei Eingabe von `5` soll die Ausgabe `1 4 9 16` sein."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b27328c4-e085-4783-8ea8-c45c62b63d9f",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Square Numbers from 0 to 1: 1\n",
"Square Numbers from 0 to 2: 1 4\n",
"Square Numbers from 0 to 3: 1 4 9\n",
"Square Numbers from 0 to 4: 1 4 9 16\n",
"Square Numbers from 0 to 5: 1 4 9 16 25\n"
]
}
],
"source": [
"def square_count(n: int) -> int: \n",
" for i in range(1, n):\n",
" yield i*i\n",
"\n",
"for n in range(2, 7):\n",
" print(f\"Square Numbers from 0 to {n-1}:\", *square_count(n))"
]
},
{
"cell_type": "markdown",
"id": "72f74416-f665-475f-a411-aa2ad5a9c257",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"### Aufgabe\n",
"\n",
"*3 Punkte*\n",
"\n",
"Schreibe einen Generator `naturals`, welcher mit jedem Aufruf die nächste natürliche Zahl ausgibt. (Angefangen mit 1)\n",
"\n",
"- Es sind keine Eingabeparameter notwendig.\n",
"- Ist die Funktion kein generator, wird diese Aufgabe mit 0 Punkten bewertet\n",
"\n",
"*Hinweis*: Orientiere dich an dem Beispiel `faktoriel_gen`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e5023e1a-1ab0-42ec-87f2-87c2eee46274",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [],
"source": [
"import types"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8769a98d-5ec0-407a-9ba0-538daff61597",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1, 2, 3, 4, 5, 6, 7, 8, 9, "
]
}
],
"source": [
"def naturals() -> int:\n",
" curr = 1\n",
" while 1:\n",
" yield curr\n",
" curr += 1\n",
" \n",
"gen: types.GeneratorType = naturals()\n",
"for i in range(1, 10):\n",
" number: int = next(gen)\n",
" print(number, end=', ')"
]
},
{
"cell_type": "markdown",
"id": "7514798b-d716-4161-a0b7-a644ac8bc67a",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"### Aufgabe\n",
"\n",
"*6 Punkte*\n",
"\n",
"Schreiben Sie eine Dataclass `Student`\n",
"\n",
"- Die dataclass soll die Werte `vorname`, `nachname`, `semester` und `mat_nr` speichern, vergebe hierzu selber den !!geeigneten!! Datentypen. Mache dir dazu Gedanken ob es Sinnvoll beispielweise die Semesteranzahl als Float zu speichern.\n",
"\n",
"- importiere aus dem dataclasses modul die Funktion `asdict`, erstelle ein Objekt mit den Werten aus dem Beispielstundent, weiße den rückgabewert aus `asdict` aufgerufen mit dem Beispielstudenten der Variablen `stud` zu.\n",
"\n",
"- Die Aufgabe wird mit 0 Punkten bewertet, wenn `Student` keine dataclass ist!\n",
"\n",
"- Hat einer der Attribute keinen geeigneten Datentypen, führt dies nicht zu Punktabzug bei zwei oder mehr schon."
]
},
{
"cell_type": "markdown",
"id": "e6d510b0-1565-489c-9441-1812153a3a9f",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"Beispielstudent:\n",
"\n",
"|Attribut|Wert|\n",
"|-|-|\n",
"|vorname|Martin|\n",
"|nachname|Le|\n",
"|mat_nr|520420|\n",
"|semester|5|"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "36bd4680-e75e-4db0-9442-9c62f393608e",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mat_nr': 520420,\n",
" 'nachname': 'Le',\n",
" 'semester': 5,\n",
" 'vorname': 'Martin'}\n"
]
}
],
"source": [
"from dataclasses import dataclass, asdict\n",
"\n",
"@dataclass\n",
"class Student:\n",
" vorname: str\n",
" nachname: str\n",
" mat_nr: int\n",
" semester: int \n",
"\n",
"student = Student(\n",
" vorname='Martin',\n",
" nachname='Le',\n",
" mat_nr=520420,\n",
" semester=5)\n",
"\n",
"stud = asdict(student)\n",
"\n",
"from pprint import pprint\n",
"pprint(stud, width=1)"
]
},
{
"cell_type": "markdown",
"id": "c1471211-26a2-4607-82de-9cc706cfc2fb",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"### Aufgabe\n",
"\n",
"*6 Punkte*\n",
"\n",
"Gegeben sind zwei Listen `colorn` & `colorv_hex`, welche zueinander Index Sortiert sind.\n",
"\n",
"Schreiben nun eine Dataclass `Color` mit den Attributen `name` & `value` und vergebe geeignete Type Hints.\n",
"\n",
"Erstelle danach eine Liste, welche die Werte aus `colorn` & `colorv_hex` in die Dataclass `Color` umwandeln, und speicher die Liste in der variablen `colors`.\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2abd79b2-2083-422b-a83d-7cd3f03aa82c",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "fragment"
},
"tags": []
},
"outputs": [],
"source": [
"colorn = ['RED', 'GREEN', 'BLUE', 'YELLOW', 'PURPLE']\n",
"colorv_hex = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF']"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "9a82261a-a644-4118-a4f2-e663f10a75bd",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Color(name='RED', value='#FF0000'),\n",
" Color(name='GREEN', value='#00FF00'),\n",
" Color(name='BLUE', value='#0000FF'),\n",
" Color(name='YELLOW', value='#FFFF00'),\n",
" Color(name='PURPLE', value='#FF00FF')]\n"
]
}
],
"source": [
"colors = None\n",
"\n",
"@dataclass\n",
"class Color:\n",
" name: str\n",
" value: str\n",
"\n",
"colors = [Color(n, w) for n, w in zip(colorn, colorv_hex)]\n",
" \n",
"pprint(colors)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long