From 7cb4b4de4f0a9540512eb1e4b79e2a799b98e3e9 Mon Sep 17 00:00:00 2001 From: Eirik Lorgen Tanberg <eirik@tanberg.org> Date: Mon, 11 Sep 2023 21:34:21 +0200 Subject: [PATCH] =?UTF-8?q?Legg=20til=20oppgaver=20for=20=C3=98F3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ovingsforelesninger/OF3/ikke_gjor_dette.ipynb | 120 +++++++ Ovingsforelesninger/OF3/tasks.ipynb | 294 ++++++++++++++++++ 2 files changed, 414 insertions(+) create mode 100644 Ovingsforelesninger/OF3/ikke_gjor_dette.ipynb create mode 100644 Ovingsforelesninger/OF3/tasks.ipynb diff --git a/Ovingsforelesninger/OF3/ikke_gjor_dette.ipynb b/Ovingsforelesninger/OF3/ikke_gjor_dette.ipynb new file mode 100644 index 0000000..3a97870 --- /dev/null +++ b/Ovingsforelesninger/OF3/ikke_gjor_dette.ipynb @@ -0,0 +1,120 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Vanlige feil\n", + "\n", + "Denne notebooken inneholder en masse vanlige feil i Python. Kjør cellene og se hva som skjer. Prøv Ã¥ forstÃ¥ hvorfor det blir feil, og hvordan du kan fikse det. ØF gÃ¥r gjennom og viser løsninger pÃ¥ disse feilene." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Feil 1: Samme navn som en funksjon" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "input = input(\"Skriv inn noe da: \")\n", + "\n", + "print(input)\n", + "\n", + "ny_input = input(\"Skriv inn noe nytt plz: \")\n", + "\n", + "print(ny_input)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Fiks\n", + "\n", + "1. Bruk `del <variabel>` for Ã¥ slette variabelen\n", + "2. Restart kernelen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "del input\n", + "\n", + "ny_input = input(\"Skriv inn noe nytt plz: \")\n", + "\n", + "print(ny_input)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Feil 2: Evig løkke/kode stopper aldri" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "KeyboardInterrupt", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[1;32m 2\u001b[0m \u001b[39mpass\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mFerdig!\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ], + "source": [ + "while True:\n", + " pass\n", + "\n", + "print(\"Ferdig!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Fiks\n", + "\n", + "1. Trykk pÃ¥ stopp-knappen/restart kernel" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "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.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Ovingsforelesninger/OF3/tasks.ipynb b/Ovingsforelesninger/OF3/tasks.ipynb new file mode 100644 index 0000000..7722977 --- /dev/null +++ b/Ovingsforelesninger/OF3/tasks.ipynb @@ -0,0 +1,294 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Øvingsforelesning 3 - TDT4110\n", + "\n", + "### Litt av hvert" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 1: Kalkulator\n", + "\n", + "Skriv et Python-program som ber brukeren om Ã¥ velge en av følgende matematiske operasjoner: \n", + " \n", + " - Addisjon\n", + " - Subtraksjon\n", + " - Multiplikasjon\n", + " - Divisjon\n", + "\n", + "Deretter skal programmet be brukeren om to tall, utføre den valgte operasjonen, og vise resultatet.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 2\n", + "\n", + "Lag en funksjon som tar inn et parameter $ð‘¥$, og returnerer det uttrykket $x^2 − 2x −3$.\n", + "\n", + "AltsÃ¥, implementer den matematiske funksjonen $f(x) = x^2 − 2x −3$ i Python." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 3\n", + "\n", + "Lag en funksjon absoluttverdi(x) som tar inn et parameter x og som returnerer absoluttverdien av x, uten Ã¥ bruke innebygde funksjoner!\n", + "\n", + "AltsÃ¥, implementer den matematiske funksjonen f(x) = |x|\n", + " - f(1) = 1\n", + " - f(-1) = 1\n", + " - f(100) = 100\n", + " - f(-100) = 100" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 4\n", + "\n", + "Du ønsker aÌŠ fremstaÌŠ sint paÌŠ et diskusjonsforum pÃ¥ nett, uten den ekstra jobben det er aÌŠ trykke paÌŠ Caps Lock\n", + "\n", + "Lag en funksjon som ber om input fra bruker, og skriver ut det den faÌŠr i STORE BOKSTAVER med et utropstegn paÌŠ slutten! \n", + "\n", + "Hint:\n", + " - str.upper() (https://docs.python.org/3/library/stdtypes.html#str.upper)\n", + "\n", + "Ekstraoppgave:\n", + " - Endre programmet til Ã¥ ta inn strengen som et parameter og returnere strengen i stedet for Ã¥ printe den ut direkte" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plotting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Kjør koden under og numpy + matplotlib blir tilgjengelig i notebooken." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.linspace(-2, 2, 5)\n", + "\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y = np.zeros(5)\n", + "\n", + "plt.plot(x, y, \"o\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Plotting av funksjon\n", + "\n", + "1. Definisjon av funksjonen $f(x) = x^2 - 2x - 3$ (gjenbrukes fra oppgave 2)\n", + "2. Lage x-akse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.linspace(-1, 2.5, 100)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3. Plot funksjon med matplotlib" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "y = f(x)\n", + "plt.plot(x, y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Subplots\n", + "\n", + "#### Object-oriented" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fig, axs = plt.subplots(1, 2)\n", + "\n", + "axs[0].plot(x, y)\n", + "axs[1].plot(x, y)\n", + "\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Pyplot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.subplot(1, 2, 1)\n", + "plt.plot(x, y)\n", + "\n", + "plt.subplot(1, 2, 2)\n", + "plt.plot(x, y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 5\n", + "\n", + "Plott funksjonen $f(x) = \\sin(x)$ i intervallet $(-4 \\pi, 4 \\pi)$. Velg “oppløsning†pÃ¥ x-aksen selv.\n", + "\n", + "Hint: Numpy har innebygget sinus-funksjon\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Oppgave 6\n", + "\n", + "Plott funksjonene $f(x) = \\sin(2x)$ og $g(x) = \\cos(2x)$ i hvert sitt plot ved siden av hverandre. Velg \"oppløsning\" pÃ¥ x-aksen selv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Skriv kode her" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "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.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab