{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Write a functions that takes two numbers as input and returns the sum." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Write a function that computes the cumulative sum of values in a sequence and returns the result as a list. Demonstrate that it works. Hint: You can use a list to pass a sequence to a function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ Write a function `factorial(n)` that returns the factorial of an integer $n$. What happens if you pass a float?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ Write a function `exp(x)` that takes a float `x` \n", "as an argument and returns $\\mathrm{e}^x$ approximated as the Taylor series up to order $n$ around the expansion point $a = 0$. Make the order $n$ an optional argument that defaults to $n = 5$. You can call `factorial(n)` from within `exp(x)` to calculate the factorials." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$T(x; a) = \\sum_{n = 0}^\\infty \\frac{f^{(n)}(a)}{n!} (x - a)^n$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For $f(x) = \\mathrm{e}^x$, $a = 0$:\n", "\n", "$$T(x; 0) = \\sum_{n = 0}^\\infty \\frac{x^{n}}{n!}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 2__ What is wrong with these two functions:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def broken():\n", " print(x)\n", " x = \"NEW\"\n", " \n", " return(x)\n", "\n", "x = \"STRING\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "run_control": { "marked": true } }, "outputs": [], "source": [ "def broken_counter():\n", " counter += 1\n", "\n", "counter = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 3__ (Warning: the solution to this problem touches a concept we have not discussed in the lesson. This is a really advanced exercise.) Ever solved a \"Jumble\"-puzzle? Write a function that expects a string\n", "and returns every possible re-combination of the letters to help you find\n", "the terms hidden behind \"rta\", \"atob\" and \"rbani\". You can test your function with the built-in function\n", "`itertools.permutations` from the `itertools` module. How many possible combinations does this return?\n", "Does the solver help you with this one: \"ibaptlienxraoc\"? Hint: Functions can call themselves. Try to pick letters from the string one by one to find a possible re-combination. " ] } ], "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.8.1" }, "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 }