{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1486c477-e52f-492a-a699-4f6668f43826",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "<div style=\"display:flex;\">\n",
    "    <div style=\"text-align: left\">\n",
    "        <h1>Lösungen Tutorial 2</h1>\n",
    "    </div>\n",
    "    <img style=\"float: right; margin: 0px 15px 15px 0px\" src=\"https://preview.redd.it/44aw5j979ve61.png?auto=webp&s=a0285d3a6e42e88b15bd738bc483412bb3efb019\" width=\"300\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89c3ed2f-6f9b-4d5c-adeb-482b4fd83b2b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe \n",
    "\n",
    "*3 Punkte*\n",
    "\n",
    "Schreibe eine Funktion `sum_up` mit Eingabeparameter `n`, welcher die Zahlen von `1...n` aufsummiert.\n",
    "\n",
    "Nutze dafür einen `for-loop`.\n",
    "\n",
    "**Beispiel**:\n",
    "\n",
    "$$n = 5$$ \n",
    "$$sum\\_up(5) \\rightarrow 1 \\rightarrow 1 + 2 = 3 \\rightarrow 3 + 3 = 6 \\rightarrow 6 + 4 = 10 \\rightarrow 10 + 5 = 15$$\n",
    "\n",
    "Hinweis: die Funktion `range()` zählt standardmässig von `0...n-1`. Schauen Sie sich gerne dazu die offizielle Dokumentation an [PEP 204](https://peps.python.org/pep-0204/#list-ranges)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6de90ca-1db9-4c59-a5a4-42016370cb35",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sum_up(n: int) -> int:\n",
    "    count = 0\n",
    "    for i in range(1,n+1):\n",
    "        count += i\n",
    "    return count\n",
    "\n",
    "sum_up(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a9de268-09e4-45e5-8aa9-db5ae79e9540",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "Es gibt immer einen besseren weg.\n",
    "\n",
    "Nach Gauß:\n",
    "\n",
    "$$\\sum_{k=1}^n k = 1 + 2 + 3 + \\dots + n = \\frac{n(n+1)}{2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fd8eea02-c356-4906-8db8-ae4cdaf4f97c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nach Gauß\n",
    "def sum_up(n: int) -> int:\n",
    "    return int(n*(n+1)/2)\n",
    "\n",
    "sum_up(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea73e1ec-7b2a-4293-b269-03b8054aaa54",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Ihnen ist das Dictionary `dict2` gegeben. Ändern Sie jeden Wert in dem Dictionary nach der Formel $f(x) = x^3-1$ mittels `for-loop`.\n",
    "\n",
    "Tipp: Lassen Sie sich nicht von den Schlüsseln verwirren."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2a3e9cac-49ee-49f0-88da-a75e9611f1d8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 56, 5: 12, 'python': 9, 3.14: 1.141414}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Vorgegeben\n",
    "dict2 = {\"a\": 56, 5: 12, \"python\": 9, 3.14: 1.141414}\n",
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26aa9f7b-b5e8-4f8f-a0d0-b796b253ad71",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 175615, 5: 1727, 'python': 728, 3.14: 0.48706374396146557}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Kurze Lösung mittels Dictionary Comprehension\n",
    "dict2 = {key: value**3-1 for key, value in dict2.items()}\n",
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4f625aaf-a04b-4c08-b18b-915b61c0145c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 175615, 5: 1727, 'python': 728, 3.14: 0.48706374396146557}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Lange Übersichtliche Lösung\n",
    "for key, value in dict2.items():\n",
    "    dict2[key] = dict2[key]**3 - 1\n",
    "dict2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4a7b4a9-7cbd-4127-8832-fdd7c3541409",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Zusatzaufgabe \n",
    "\n",
    "*Keine Punkte*\n",
    "\n",
    "Erstellen Sie eine List mittels List Comprehension, welche die Zahlen `1...6` auf deren kubische Zahl `1...216` also der Funktion $f(x) = x^3$ abbildet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f8e84221-f7da-474a-aa14-bcf7d1d3a201",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125, 216]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cubics = [n**3 for n in range(1,7)]\n",
    "cubics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cdbb5b7-f828-48eb-ba49-09d5ae68c2d7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Erstellen und Öffnen sie eine Datei `testfile.txt` mit der `open` Funktion, nutzen Sie dafür das `with`-Statement.\n",
    "\n",
    "Schreiben Sie in diese Datei 100 mal den String `\"Python\\n\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "90875b44-8209-498a-ad38-063772588711",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open('testfile.txt', 'w') as f:\n",
    "    for _ in range(100):\n",
    "        f.write(\"Python\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c69ac8e9-34ef-413a-a39f-7db6a830cde3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Öffnen Sie die zuvor erstellte Datei `testfile.txt` im Lesemodus und weißen Sie den Inhalt der `.readlines()` Funktion der Variabeln `lines` zu. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a8395ec5-59dc-43e3-a59d-9dea7cf1f21d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anzahl der gelesenen Zeilen: 100\n"
     ]
    }
   ],
   "source": [
    "lines = None # vorgegeben\n",
    "with open('testfile.txt', 'r') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "print(\"Anzahl der gelesenen Zeilen:\", len(lines))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00561f0d-9d9f-45bd-bf1f-824f04edf795",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*3 Punkte*\n",
    "\n",
    "Importiere Python Built-In Library `random` und rufe zuerst aus dem Modul die Funktion `seed` auf mit dem Eingabewert `42`, und weiße danach der Variable `rand` den Wert des Funktionsaufrufes von `randint(1,100)` zu. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "63c44277-581a-426c-8e2d-ed911688ca91",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "82"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand = None # vorgegeben\n",
    "import random\n",
    "random.seed(42)\n",
    "rand = random.randint(1,100)\n",
    "rand"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78ec5791-46b7-4693-b276-7f5e73a18eb7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*1 Punkt*\n",
    "\n",
    "Importieren Sie die Built-In Library `datetime` als `dt`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e3f4fe99-5980-4c2a-be4e-ae777f4bbb3d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-11-08 11:58:43.750641\n"
     ]
    }
   ],
   "source": [
    "import datetime as dt\n",
    "print(dt.datetime.now())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1bf6ca3-4f96-456e-a8e9-34d9fd081c5b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "### Aufgabe\n",
    "\n",
    "*2 Punkte*\n",
    "\n",
    "Importieren Sie die Funktion `sqrt` aus dem Built-In Modul `math`.\n",
    "Berechnen Sie $\\sqrt4$. Speichern Sie das Ergebnis in der variablen `s4`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "eed3fc0f-67c1-43d5-ac2f-f6b7646aecdd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from math import sqrt\n",
    "s4 = sqrt(4)\n",
    "s4"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
