{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming with Python\n",
"#### Stefan Güttel, [guettel.com](http://guettel.com)\n",
"\n",
"## Exercises: Functions\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"\n",
"**Important information:** In most of the upcoming problems you will be asked to write functions with their names being indicated in the problem. It is very important to use exactly the names provided (for example, `myfunction()`) as you will be able to reuse your functions in future exercises and also because our in-class tests are automatically graded. If you do not use the exact name as required (for example using `myFunction()`) your function will not be executable and you will lose marks. Also, when asked to write functions, make it a habit to write a function `main()` where all the testing is done. This `main()` function should define meaningful input parameters, call the functions, and output the result for testing. \n",
"\n",
"Here is a simple example where we have been asked to write functions `myfunction1()` and `myfunction2()` and we are testing both of them using a `main()` function:\n",
"\n",
" def myfunction1(a, b):\n",
" \"\"\"\n",
" Takes two input arguments a and b and returns their sum.\n",
" \"\"\"\n",
" s = a + b\n",
" return s\n",
" \n",
" def myfunction2(a, b):\n",
" \"\"\"\n",
" Takes two input arguments a and b and returns their product.\n",
" \"\"\"\n",
" s = a * b\n",
" return s\n",
" \n",
" def main():\n",
" a = 5\n",
" b = 3\n",
" print(myfunction1(a, b)) # hopefully outputs 8\n",
" print(myfunction2(a, b)) # hopefully outputs 15\n",
" \n",
" main()\n",
"\n",
"\n",
"***\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 1.** Write a function `max_digit(n, d, r)` that works like `digit_sum` from the lectures but, instead of returning the sum of the digits of `n`, returns the maximum digit of `n` that gives the reminder `r` when divided by `d`. Assign the appropriate default values for any of the parameters for which it makes sense to do so.\n",
"\n",
"Notice that this maximum is undefined for some numbers. Situations like this are usually handled with exceptions, which will be covered later in this course. For now, detect the problematic case(s) and return an invalid value `-1`.\n",
"\n",
"Write a `main()` function that tests this function, i.e., assigns some values to variables `n`, `d`, and `r`, and then prints the maximum digit of `n` that gives the reminder `r` when divided by `d`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 2.** Write a function `binom(n, k)` that returns the [binomial coefficient](http://en.wikipedia.org/wiki/Binomial_coefficient)\n",
"$$\\binom{n}{k} := \\frac{n!}{k!(n-k)!}$$\n",
"for given natural numbers `n` and `k`. Return zero when $n < 0$ or $k < 0$.\n",
"\n",
"Be careful to compute and return an integer (not a floating point number).\n",
"\n",
"Also, write `binomf(n, k)` which does the same thing using floating point arithmetic (but otherwise exactly the same algorithm).\n",
"\n",
"Write a `main()` function that assigns some values to `n` and `k`, calls `binom(n, k)` and `binomf(n, k)` and prints their values, and the difference of these values. Test what happens for fairly large inputs (for example, `(n, k) = (100, 50)`).\n",
"\n",
"If you want to make a more general function that will work correctly for all real values of $n$, you can check your results at [Wolfram|Alpha](http://www.wolframalpha.com/), for example like [this](http://www.wolframalpha.com/input/?i=binomial%28100%2C50%29)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 3.** Write a function `prime(n)` that returns `True` if `n` is a [prime number](http://en.wikipedia.org/wiki/Prime_number) and `False` otherwise. You may use the definition as a criterion:\n",
"\n",
"> A number $n$ is prime if it is a natural number with exactly two distinct divisors. \n",
"> Equivalently, $n$ is prime if $n > 1$ and is not divisible by any integer strictly between 1 and `n`.\n",
"\n",
"Write a `main()` function that loads integers until it gets a negative number or a zero. Using the function `prime(n)`, the `main()` function should print the product of all the loaded prime numbers. If no primes were loaded, the function should instead print an appropriate descriptive message."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 4.** We say that a number $n$ is *rich* if its absolute value is smaller than the sum of all its divisors except itself. For example, $12 < 1+2+3+4+6 = 16$ is considered rich, while $16 > 1+2+4+8 = 15$ is not.\n",
"\n",
"Write a function `rich(n)` that returns `True` if `n` is rich and `False` otherwise.\n",
"\n",
"Further, write a `main()` function that assigns a number $k \\in \\mathbb{N}$ and prints all the rich numbers between $1$ and $k$ (inclusive).\n",
"\n",
"For example, the rich numbers smaller than $50$ are: $12, 18, 20, 24, 30, 36, 40, 42, 48$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Recursive functions\n",
"\n",
"The next two problems go beyond the scope of the course. However, they may be useful, especially for students interested in combinatorics.\n",
"\n",
"**These two will not be part of any exams!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 5.** $\\color{red}{\\star}$ Write a function `f` that takes an integer parameter `x` and returns the value\n",
"$$f(x) = \\begin{cases}\n",
" f(17 - |x|), & x < 2, \\\\\n",
" f(d), & \\text{$x$ is a composite number and $d$ is its largest divisor such that $d < x$}, \\\\\n",
" x, & \\text{otherwise}.\n",
"\\end{cases}$$\n",
"We say that $x$ is a composite number if $x > 1$ and $x$ is not a prime.\n",
"\n",
"Write a `main()` function that loads `x` until it loads `x = 0`, and then writes the value `f(x)` for each of the loaded numbers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 6.** $\\color{red}{\\star}$ Write a function `f` that takes an integer parameter `x` and returns the value\n",
"$$f(x) = \\begin{cases}\n",
" x^2, & x < 9, \\\\\n",
" f(g(x)), & \\text{$x \\ge 9$ is even}, \\\\\n",
" f(h(x+1)), & \\text{otherwise},\n",
"\\end{cases}$$\n",
"where $g(x) = \\left\\lceil \\frac{x}{2} \\right\\rceil$ is the [ceiling](http://en.wikipedia.org/wiki/Floor_and_ceiling_functions) of $\\frac{x}{2}$ and $h(x) = \\left\\lfloor \\frac{x}{2} + 1 \\right\\rfloor$ is the floor of $\\frac{x}{2}+1$.\n",
"\n",
"Write a `main()` function that loads `x` until it loads `x = 0`, and then writes the value `f(x)` for each of the loaded numbers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Hints for Problems 5 and 6:**\n",
"\n",
"* These are simple examples of [recursive functions](http://en.wikipedia.org/wiki/Recursion_%28computer_science%29#Recursive_functions_and_algorithms). A function in Python (and most other modern languages) can invoke itself just like it can invoke any other function. However, there are two major things to remember:\n",
" * As we have mentioned before, the local variables are local to each invocation. So, if a function that invokes itself has a parameter `x`, then there will be two variables `x` in the memory, each belonging to its own invocation. The two are in no way related (apart from having the same name), and each invocation will see only its own `x`.\n",
" * There must exist values of the input parameters for which the function does not invoke itself (so called *terminating cases*). Without that, the function will invoke itself indefinitely (or, more precisely, until the program runs out of the available memory).\n",
"\n",
"* Here is a sketch how such a function may look:\n",
"\n",
" ```python3\n",
" def f(x):\n",
" if some_condition:\n",
" return f(some_expression)\n",
" if some other condition:\n",
" return f(some_other_expression)\n",
" ...\n",
" return an_expression_that_covers_the_case_when_no_prior_condition_was_met\n",
" ```\n",
"\n",
"* Write auxiliary functions (the one for finding $d$ in Problem 5, $h$ and $g$ in Problem 6), to make it easier to organize and read your code."
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}