{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A7 – Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Create an empty list and fill it iteratively with even numbers from 2 to 12 converted to floats." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2.0, 4.0, 6.0, 8.0, 10.0, 12.0]\n" ] } ], "source": [ "l = []\n", "for i in range(2, 13, 2):\n", " l.append(float(i))\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Consider again the discrete potential function from the lesson. Find out if the function has a potential energy minimum and print out the $x$-value of the minimum if you found one." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "E: [98, 79, 62, 47, 34, 23, 14, 7, 2, -1, -2, -1, 2, 7, 14, 23, 34, 47, 62, 79, 98]\n" ] } ], "source": [ "x = list(range(-10, 11))\n", "E = [x**2 - 2 for x in x]\n", "print(\"x: \", x)\n", "print(\"E: \", E)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "E(-10): 98, E(-9): 79, Slope: -/-\n", "E(-9): 79, E(-8): 62, Slope: -/-\n", "E(-8): 62, E(-7): 47, Slope: -/-\n", "E(-7): 47, E(-6): 34, Slope: -/-\n", "E(-6): 34, E(-5): 23, Slope: -/-\n", "E(-5): 23, E(-4): 14, Slope: -/-\n", "E(-4): 14, E(-3): 7, Slope: -/-\n", "E(-3): 7, E(-2): 2, Slope: -/-\n", "E(-2): 2, E(-1): -1, Slope: -/-\n", "E(-1): -1, E(0): -2, Slope: -/-\n", "E(0): -2, E(1): -1, Slope: -/+\n", "Minimum around x = 0\n", "E(1): -1, E(2): 2, Slope: +/+\n", "E(2): 2, E(3): 7, Slope: +/+\n", "E(3): 7, E(4): 14, Slope: +/+\n", "E(4): 14, E(5): 23, Slope: +/+\n", "E(5): 23, E(6): 34, Slope: +/+\n", "E(6): 34, E(7): 47, Slope: +/+\n", "E(7): 47, E(8): 62, Slope: +/+\n", "E(8): 62, E(9): 79, Slope: +/+\n", "E(9): 79, E(10): 98, Slope: +/+\n" ] } ], "source": [ "slope_map = {True: \"+\", False: \"-\"}\n", "\n", "current = 0 # Current index\n", "slope = False # Negative slope (we just assume this for simplicity)\n", "\n", "# Go through the list of energies (starting at second element)\n", "for n, next_ in enumerate(E[1:], 1):\n", " # What is the slope to the next point?\n", " if next_ < E[current]: \n", " next_slope = False # Negative slope\n", " elif next_ > E[current]:\n", " next_slope = True # Positive slope\n", " else:\n", " next_slope = slope # no change in slope\n", " \n", " print(f\"E({x[current]}): {E[current]}, E({x[n]}): {next_}, \"\n", " f\"Slope: {slope_map[slope]}/{slope_map[next_slope]}\")\n", " \n", " if (next_slope is True) and (slope is False):\n", " print(f\"Minimum around x = {x[current]}\")\n", " elif (next_slope is False) and (slope is True):\n", " print(f\"Maximum around x = {x[current]}\") \n", " \n", " slope = next_slope\n", " current += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ Given an integer `n` compute the factorial of this number." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "n = 5\n", "\n", "factorial = 1\n", "while n > 1:\n", " factorial *= n\n", " n -= 1\n", "\n", "print(factorial)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ Take the following dummy text and create a dictionary that \n", "has the occurring letters (characters) as keys and the corresponding letter counts as values (example below)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "\"\"\"As any dedicated reader can clearly see, the Ideal of practical reason is a representation\n", "of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena\n", "should only be used as a canon for our understanding. The paralogisms of practical\n", "reason are what first give rise to the architectonic of practical reason. As will easily be\n", "shown in the next section, reason would thereby be made to contradict, in view of these\n", "considerations, the Ideal of practical reason, yet the manifold depends on the phenomena.\n", "Necessity depends on, when thus treated as the practical employment of the never-ending\n", "regress in the series of empirical conditions, time. Human reason depends on our sense\n", "perceptions, by means of analytic unity. There can be no doubt that the objects in space\n", "and time are what first give rise to human reason.\"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Example for count dict\n", "counts = {\n", " \"a\": 12,\n", " \"b\": 1\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "text = \"\"\"As any dedicated reader can clearly see, the Ideal of practical reason is a representation\n", "of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena\n", "should only be used as a canon for our understanding. The paralogisms of practical\n", "reason are what first give rise to the architectonic of practical reason. As will easily be\n", "shown in the next section, reason would thereby be made to contradict, in view of these\n", "considerations, the Ideal of practical reason, yet the manifold depends on the phenomena.\n", "Necessity depends on, when thus treated as the practical employment of the never-ending\n", "regress in the series of empirical conditions, time. Human reason depends on our sense\n", "perceptions, by means of analytic unity. There can be no doubt that the objects in space\n", "and time are what first give rise to human reason.\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'A': 2, 's': 54, ' ': 134, 'a': 61, 'n': 60, 'y': 11, 'd': 26, 'e': 100, 'i': 44, 'c': 29, 't': 55, 'r': 42, 'l': 22, ',': 11, 'h': 32, 'I': 4, 'o': 49, 'f': 14, 'p': 17, '\\n': 9, 'k': 1, 'w': 10, 'g': 7, 'm': 14, 'v': 6, ';': 1, 'u': 11, 'b': 8, '.': 6, 'T': 2, 'x': 1, 'N': 1, '-': 1, 'H': 1, 'j': 1}\n" ] } ], "source": [ "counts = {}\n", "for letter in text:\n", " if letter in counts:\n", " counts[letter] += 1\n", " continue\n", " counts[letter] = 1\n", "\n", "print(counts)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }