From 1bfd99c92f3ee963c8e43aac90290d9f1539a311 Mon Sep 17 00:00:00 2001 From: Xanthron Writer Date: Wed, 31 May 2023 10:01:41 +0200 Subject: [PATCH] =?UTF-8?q?Weitere=20Aufgabe=20f=C3=BCr=20=C3=9Cbung=20Ope?= =?UTF-8?q?ratoren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ich habe eine Aufgabe für die Übung Operatoren hinzugefügt. Sie soll den Studenten die Kombination von `and` und `or` beibringen und gleichzeitig das Thema Zahlendarstellung anschneiden und Binärsystem. --- .../02_operationen/operationen.ipynb | 130 +++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/book/content/Exercises/02_python/02_operationen/operationen.ipynb b/book/content/Exercises/02_python/02_operationen/operationen.ipynb index 83fbf25d..aaeebad5 100644 --- a/book/content/Exercises/02_python/02_operationen/operationen.ipynb +++ b/book/content/Exercises/02_python/02_operationen/operationen.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -24,6 +25,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -31,6 +33,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -38,6 +41,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -49,6 +53,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -56,6 +61,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -70,6 +76,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -216,6 +223,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -223,6 +231,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -235,6 +244,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -247,6 +257,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -284,6 +295,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -312,6 +324,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -348,6 +361,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -377,6 +391,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -386,6 +401,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ @@ -440,6 +456,118 @@ "print(\"Das Abstandsquadrat ist: \", dist2)\n", "print(\"Die Koordinaten des Mittelpunkts sind: (\", mx, \",\", my, \")\")" ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Aufgabenteil D\n", + "Speicherverbrauch und Performance ist bei komplexen Algorithmen ein wichtiges Thema und müssen auf den jeweiligen Fall angepasst werden. Eine Optimierung wäre z.B. statt einer Liste von Booleans eine Zahl zu verwenden, bei der jeder Bit als Wahr oder Falsch interpretiert werden kann.\n", + "\n", + "Überprüfen sie ob ...\n", + "1. ... mindestens ein Bit von `10100101` wahr ist.\n", + "2. ... alle Bits von `10100101` falsch sind.\n", + "3. ... mindestens ein Bit von `10100101` falsch ist\n", + "4. ... alle Bits von `10100101` wahr sind.\n", + "\n", + "Wie würde ein Programm aussehen, bei dem die einzeln Bits als `True` und `False` ausgeschrieben werden?" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lösungshinweis\n", + "Um eine Zahl im Binärsystem zu definieren wird das Präfix `0b` verwendet." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'10100101' im Dezimalsystem: 165\n" + ] + } + ], + "source": [ + "print(\"'10100101' im Dezimalsystem:\", 0b10100101)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "tags": [ + "loesung", + "hide-cell" + ] + }, + "source": [ + "### Lösungsvorschlag" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "tags": [ + "loesung", + "hide-cell" + ] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mindestens ein Werte von '10100101' ist wahr: True\n", + "Alle Werte von '10100101' sind falsch: False\n", + "Mindestens ein Werte von '10100101' ist falsch: True\n", + "Alle Werte von '10100101' sind wahr: False\n" + ] + } + ], + "source": [ + "print(\"Mindestens ein Bit von '10100101' ist wahr:\", 0b10100101 > 0)\n", + "print(\"Alle Bits von '10100101' sind falsch:\", 0b10100101 == 0)\n", + "print(\"Mindestens ein Bit von '10100101' ist falsch:\", 0b10100101 < 0b11111111)\n", + "print(\"Alle Bits von '10100101' sind wahr:\", 0b10100101 == 0b11111111)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "tags": [ + "loesung", + "hide-cell" + ] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mindestens ein Werte von '10100101' ist wahr: True\n", + "Alle Werte von '10100101' sind falsch: False\n", + "Mindestens ein Werte von '10100101' ist falsch: True\n", + "Alle Werte von '10100101' sind wahr: False\n" + ] + } + ], + "source": [ + "print(\"Mindestens ein Bit von '10100101' ist wahr:\", (True or False or True or False or False or True or False or True) == True)\n", + "print(\"Alle Bits von '10100101' sind falsch:\", (True or False or True or False or False or True or False or True) == False)\n", + "print(\"Mindestens ein Bit von '10100101' ist falsch:\", (True and False and True and False and False and True and False and True) == False)\n", + "print(\"Alle Bits von '10100101' sind wahr:\", (True and False and True and False and False and True and False and True) == True)" + ] } ], "metadata": { @@ -459,7 +587,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.10.10" } }, "nbformat": 4,