Tutorial A8

1 Write a functions that takes two numbers as input and returns the sum.

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.

3 Write a function factorial(n) that returns the factorial of an integer \(n\). What happens if you pass a float?

Advanced 1 Write a function exp(x) that takes a float x 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.

\[T(x; a) = \sum_{n = 0}^\infty \frac{f^{(n)}(a)}{n!} (x - a)^n\]

For \(f(x) = \mathrm{e}^x\), \(a = 0\):

\[T(x; 0) = \sum_{n = 0}^\infty \frac{x^{n}}{n!}\]

Advanced 2 What is wrong with these two functions:

[11]:
def broken():
    print(x)
    x = "NEW"

    return(x)

x = "STRING"
[12]:
def broken_counter():
    counter += 1

counter = 1

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 and returns every possible re-combination of the letters to help you find the terms hidden behind “rta”, “atob” and “rbani”. You can test your function with the built-in function itertools.permutations from the itertools module. How many possible combinations does this return? 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.