1. Write a function that returns the Pythagorean triplet (a, b, c)
given any two of the arguments a
, b
or c
.
For example, triplet(a=3, c=5)
will return (3, 4, 5).
2. The $n^\text{th}$ Catalan number is given by $$ C_n = \prod_{k=2}^n \frac{n+k}{k} $$ for $n \ge 0$.
Write a function to calculate the $n^\text{th}$ Catalan number - it should take a single argument $n$ and return $C_n$. Use this function to generate a list of the first 10 odd Catalan numbers (i.e., n = 1, 3, 5, ...).
3. Rewrite the above function as a lambda
function. This is tricky but introduces you to the functional style that is very useful in advanced programming. Some imports are done for you as a hint.
In [16]:
from functools import reduce
from operator import mul
4. Write a class called Matrix that can be initialized with an iterable of numbers, together with the number of rows r and the number of columns c. That is, you should be able to create a matrix M
by calling M = Matrix([1,2,3,4], 2, 2)
(assume the matrix is row ordered). The Matrix class will have a single additional method called at
that takes a tuple (r, c)
and returns the value at row r
and column c
. For example, M.at(0,1)
will return 2. Internally, the data should be stored as a list and the at
method will use indexing to find the right value to return.