Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion book/content/Exercises/02_python/05_schleifen/schleifen.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"tags": [
Expand All @@ -24,27 +25,31 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Anwendung von Schleifen"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"In dieser Aufgabe wird die Verwendung von Schleifen anhand der Summation von ganzen Zahlen und in einer Umrechnungstabelle geübt."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aufgabenteil A"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -56,20 +61,23 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lösungshinweis"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Für $\\sf N=100$ lautet das Ergebnis 5050. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"tags": [
Expand Down Expand Up @@ -180,13 +188,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aufgabenteil B"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -198,13 +208,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lösungshinweis"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -257,6 +269,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"tags": [
Expand Down Expand Up @@ -320,6 +333,101 @@
" ms = v * 1000 / 3600\n",
" print(v, '\\t', ms)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aufgabenteil C\n",
"In der Vorherigen Übung sollte in einer Aufgabe eine Liste mit einem bestimmten Muster erstellt werden. Mit dem neu erlernten Konzept von schleifen, ist es nun möglich durch das verändern von zwei Variablen jedes mögliche Muster mit **N** Zahlen nach gleichem Prinzip zu erstellen.\n",
"\n",
"Schreiben Sie ein Skript, das eine Liste mit Zahlen von 0 bis **N** ausgibt. Jedoch sollen nur Zahlen in einem bestimmten Muster in der Liste vorhanden sein. **z1** zahlen hinzufügen, danach **z2** Zahlen überspringen, danach wieder **z1** einfügen Zahlen, um darauf die nächsten **z2** wieder zu überspringen, usw. bis zur Zahl **N**."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lösungsvorschlag"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"tags": [
"loesung",
"hide-cell"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 28, 29]\n"
]
}
],
"source": [
"N = 30\n",
"z1 = 4\n",
"z2 = 3\n",
"liste = []\n",
"\n",
"for z in range(z1):\n",
" liste += list(range(z, N, z1 + z2))\n",
"\n",
"liste.sort()\n",
"print(liste)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Um den Umgang mit Schleifen weiter zu festigen, soll die obrige Liste ohne das Sortieren am Ende erstellt werden.\n",
":::::{tip}\n",
"::::{toggle}\n",
"Nutzen Sie eine `for`-Schleife um Elemente in eine Liste einzufügen. Was passiert auf diese Weise jedoch bei `(N-1) % (z1 + z2) < z2` und welche maßnahme muss getroffen werden?\n",
":::{toggle}\n",
"Entfernen Sie mit einer `while`-Schleife die Überschüssigen Elemente\n",
":::\n",
"::::\n",
":::::"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"tags": [
"loesung",
"hide-cell"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 28, 29, 30, 31]\n"
]
}
],
"source": [
"liste = []\n",
"for i in range(0, N, z1 + z2):\n",
" for j in range(0, z1):\n",
" liste.append(i+j)\n",
"\n",
"while liste[-1] >= N:\n",
" liste.pop()\n",
"print(liste)"
]
}
],
"metadata": {
Expand All @@ -339,7 +447,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down