From ad7bc97062bd6e0d1bf5090e724f4963df7fe8bd Mon Sep 17 00:00:00 2001 From: schwabmi <michel.j.schwab@gmail.com> Date: Wed, 16 Dec 2020 11:35:13 +0100 Subject: [PATCH] exchanged exkurse -> weihnachtsbaum --- notebooks/seminar05.ipynb | 98 ++++++++++++++++++++++++++------------- notebooks/seminar07.ipynb | 91 +++++++++++++----------------------- 2 files changed, 98 insertions(+), 91 deletions(-) diff --git a/notebooks/seminar05.ipynb b/notebooks/seminar05.ipynb index 66b888b..f2a0a07 100644 --- a/notebooks/seminar05.ipynb +++ b/notebooks/seminar05.ipynb @@ -20,9 +20,7 @@ "(Buch: http://greenteapress.com/thinkpython2/html/thinkpython2006.html)\n", "## Exkurs: Was mir an Python gefällt\n", "\n", - "In dieser Rubrik, die immer am Anfang eines Kapitels steht, möchte ich Ihnen zeigen, wofür ich Python nutze und warum ich es mag. Sie werden vielleicht noch nicht verstehen, was ich genau mache, aber Sie sehen damit schon einmal die Möglichkeiten von Python und können später darauf zurückgreifen. Da dies auch ein Exkurs ist, können Sie diese Rubrik gerne auch erst einmal überspringen.\n", - "\n", - "Mit den Operatoren aus diesem Kapitel können wir ganz leicht das Verfahren zur Umwandlung einer Dezimalzahl in ihre Binärdarstellung implementieren:" + "Wir wünschen Ihnen ein frohes Fest und einen guten Rutsch ins neue Jahr." ] }, { @@ -31,37 +29,73 @@ "metadata": {}, "outputs": [], "source": [ - "# Umwandlung einer positiven, ganzen Dezimalzahl in Binärdarstellung (als Zeichenkette)\n", - "def dez_zu_bin(n):\n", - " ergebnis = \"\"\n", - " while n > 0:\n", - " ergebnis = str(n % 2) + ergebnis\n", - " n = n // 2\n", - " return ergebnis\n", - "\n", - "print(dez_zu_bin(42))\n", - "\n", - "# Und weil wir heute beim Thema Rekursion sind ...\n", - "def dez_zu_bin_rekursiv(n):\n", - " if n == 0:\n", - " return \"\"\n", - " return dez_zu_bin_rekursiv(n // 2) + str(n % 2)\n", - "\n", - "print(dez_zu_bin_rekursiv(42))\n", - "\n", - "# Warum eigentlich auf ein Zahlensystem festlegen?\n", - "def dez_zu_allem(n, s):\n", - " if n == 0:\n", - " return \"\"\n", - " return dez_zu_allem(n // len(s), s) + s[n % len(s)]\n", - "\n", - "print(dez_zu_allem(42, \"01\"))\n", - "print(dez_zu_allem(42, \"0123456789\"))\n", - "print(dez_zu_allem(42, \"01234567\"))\n", - "print(dez_zu_allem(42, \"0123456789ABCDEF\"))\n", - "print(dez_zu_allem(42, \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"))" + "\"\"\" \n", + "Quelle: https://teampython.wordpress.com/2015/12/12/print-a-christmas-tree/\n", + "Python 3 version by antiloquax (2015), based on code from datamungeblog.com.\n", + "\"\"\"\n", + " \n", + "from random import choice\n", + "from random import random\n", + " \n", + "# If you change this, use an odd number.\n", + "size = 21\n", + "\n", + "# Probability that a character will be green.\n", + "prob_gr = 0.6\n", + "# Colour codes.\n", + "colours = [31, 33, 34, 35, 36, 37]\n", + "# Characters to use for decorations. Experiment with these.\n", + "# The chr(169) and chr(174) characters may not work in all terminals\n", + "# (extended ASCII, c and r in a circle).\n", + "decs = ['@', '&', '*', chr(169), chr(174)]\n", + "\n", + "# Format string for printing blinking characters.\n", + "blink_col = \"\\033[5;{0}m{1}\\033[0m\"\n", + "# String to print a green octothorpe ('#').\n", + "leaf = \"\\033[32m#\\033[0m\"\n", + "\n", + "# Width of the tree, will grow by 2 each time.\n", + "width = 1\n", + "# Initialise the tree string, with a star at the top.\n", + "tree = \"\\n{}*\\n\".format(' ' * (size))\n", + "\n", + "\"\"\" Main Loop starts now.\"\"\"\n", + " \n", + "\"\"\" We can't use the normal \"format\" centering approach:\n", + " (\"{:^nn}\".format(string) where \"nn\" is the width of the line), \n", + " with these ansi codes. This is because Python sees the strings as being\n", + " more than one character long (15 & 10 for baubles and leaves).\"\"\"\n", + "\n", + "# Loop from (size - 1) down to 0, using the counter as the padding size.\n", + "for pad in range(size - 1, -1, -1):\n", + " # Increase the width of the tree by 2.\n", + " width += 2\n", + " \n", + " # Put the characters for the line in \"temp\".\n", + " temp = \"\"\n", + " for j in range(width):\n", + " # Make some leaves.\n", + " if random() < prob_gr:\n", + " temp += leaf\n", + " # And also some baubles.\n", + " else:\n", + " temp += blink_col.format(choice(colours), choice(decs))\n", + "\n", + " # Add that string to the line, with padding.\n", + " tree += \"{0}{1}\\n\".format(' ' * pad, temp)\n", + "\n", + "# Add a \"trunk\" of 2 lines and return.\n", + "print(tree + \"{0}{1}\\n\".format(' ' * (size - 1), \"000\") * 2)\n", + "print(\"\\x46\\x72\\x6f\\x68\\x65\\x20\\x46\\x65\\x73\\x74\\x74\\x61\\x67\\x65\\x21\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/seminar07.ipynb b/notebooks/seminar07.ipynb index 550bd18..13fde6b 100644 --- a/notebooks/seminar07.ipynb +++ b/notebooks/seminar07.ipynb @@ -29,7 +29,9 @@ "source": [ "## Exkurs: Was mir an Python gefällt\n", "\n", - "Wir wünschen Ihnen ein frohes Fest und einen guten Rutsch ins neue Jahr." + "In dieser Rubrik, die immer am Anfang eines Kapitels steht, möchte ich Ihnen zeigen, wofür ich Python nutze und warum ich es mag. Sie werden vielleicht noch nicht verstehen, was ich genau mache, aber Sie sehen damit schon einmal die Möglichkeiten von Python und können später darauf zurückgreifen. Da dies auch ein Exkurs ist, können Sie diese Rubrik gerne auch erst einmal überspringen.\n", + "\n", + "Mit den Operatoren aus diesem Kapitel können wir ganz leicht das Verfahren zur Umwandlung einer Dezimalzahl in ihre Binärdarstellung implementieren:" ] }, { @@ -38,64 +40,35 @@ "metadata": {}, "outputs": [], "source": [ - "\"\"\" \n", - "Quelle: https://teampython.wordpress.com/2015/12/12/print-a-christmas-tree/\n", - "Python 3 version by antiloquax (2015), based on code from datamungeblog.com.\n", - "\"\"\"\n", - " \n", - "from random import choice\n", - "from random import random\n", - " \n", - "# If you change this, use an odd number.\n", - "size = 21\n", - "\n", - "# Probability that a character will be green.\n", - "prob_gr = 0.6\n", - "# Colour codes.\n", - "colours = [31, 33, 34, 35, 36, 37]\n", - "# Characters to use for decorations. Experiment with these.\n", - "# The chr(169) and chr(174) characters may not work in all terminals\n", - "# (extended ASCII, c and r in a circle).\n", - "decs = ['@', '&', '*', chr(169), chr(174)]\n", - "\n", - "# Format string for printing blinking characters.\n", - "blink_col = \"\\033[5;{0}m{1}\\033[0m\"\n", - "# String to print a green octothorpe ('#').\n", - "leaf = \"\\033[32m#\\033[0m\"\n", - "\n", - "# Width of the tree, will grow by 2 each time.\n", - "width = 1\n", - "# Initialise the tree string, with a star at the top.\n", - "tree = \"\\n{}*\\n\".format(' ' * (size))\n", - "\n", - "\"\"\" Main Loop starts now.\"\"\"\n", - " \n", - "\"\"\" We can't use the normal \"format\" centering approach:\n", - " (\"{:^nn}\".format(string) where \"nn\" is the width of the line), \n", - " with these ansi codes. This is because Python sees the strings as being\n", - " more than one character long (15 & 10 for baubles and leaves).\"\"\"\n", - "\n", - "# Loop from (size - 1) down to 0, using the counter as the padding size.\n", - "for pad in range(size - 1, -1, -1):\n", - " # Increase the width of the tree by 2.\n", - " width += 2\n", - " \n", - " # Put the characters for the line in \"temp\".\n", - " temp = \"\"\n", - " for j in range(width):\n", - " # Make some leaves.\n", - " if random() < prob_gr:\n", - " temp += leaf\n", - " # And also some baubles.\n", - " else:\n", - " temp += blink_col.format(choice(colours), choice(decs))\n", - "\n", - " # Add that string to the line, with padding.\n", - " tree += \"{0}{1}\\n\".format(' ' * pad, temp)\n", - "\n", - "# Add a \"trunk\" of 2 lines and return.\n", - "print(tree + \"{0}{1}\\n\".format(' ' * (size - 1), \"000\") * 2)\n", - "print(\"\\x46\\x72\\x6f\\x68\\x65\\x20\\x46\\x65\\x73\\x74\\x74\\x61\\x67\\x65\\x21\")" + "# Umwandlung einer positiven, ganzen Dezimalzahl in Binärdarstellung (als Zeichenkette)\n", + "def dez_zu_bin(n):\n", + " ergebnis = \"\"\n", + " while n > 0:\n", + " ergebnis = str(n % 2) + ergebnis\n", + " n = n // 2\n", + " return ergebnis\n", + "\n", + "print(dez_zu_bin(42))\n", + "\n", + "# Und weil wir heute beim Thema Rekursion sind ...\n", + "def dez_zu_bin_rekursiv(n):\n", + " if n == 0:\n", + " return \"\"\n", + " return dez_zu_bin_rekursiv(n // 2) + str(n % 2)\n", + "\n", + "print(dez_zu_bin_rekursiv(42))\n", + "\n", + "# Warum eigentlich auf ein Zahlensystem festlegen?\n", + "def dez_zu_allem(n, s):\n", + " if n == 0:\n", + " return \"\"\n", + " return dez_zu_allem(n // len(s), s) + s[n % len(s)]\n", + "\n", + "print(dez_zu_allem(42, \"01\"))\n", + "print(dez_zu_allem(42, \"0123456789\"))\n", + "print(dez_zu_allem(42, \"01234567\"))\n", + "print(dez_zu_allem(42, \"0123456789ABCDEF\"))\n", + "print(dez_zu_allem(42, \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"))" ] }, { -- GitLab