{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming with Python\n",
"#### Stefan Güttel, [guettel.com](http://guettel.com)\n",
"\n",
"## Lab classes: Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"\n",
"**Reminder:** 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",
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 1.** Write a function `main()` that loads a natural number `n` and then `n` integers. The function has to compute the sum of the digits of each of these numbers, and then the product of these sums. In the end, the function has to print the sum of the digits of the computed product.\n",
"\n",
"More formally, if $\\operatorname{sd}(x)$ denotes the sum of the digits of $x$, and $x_1,x_2,\\dots,x_n$ denote the loaded numbers, then the desired output is\n",
"$$\\operatorname{sd}\\left(\\prod_{k=1}^n \\operatorname{sd}(x_k)\\right).$$\n",
"Your `main()` should make use of a function `sd(x)` which returns the sum of the digits of its input argument `x`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 2. ** Write a function `sdn` takes integer arguments `n` and `r`. If $r \\le 0$, it returns $n$. If $r > 0$, it returns\n",
"$$\\operatorname{sd}^r(n) := (\\underbrace{\\operatorname{sd} \\circ \\operatorname{sd} \\circ \\cdots \\circ \\operatorname{sd}}_{r})(n) = \\underbrace{\\operatorname{sd}(\\operatorname{sd}(\\dots\\operatorname{sd}}_{r}(n)\\dots)),$$\n",
"where $\\operatorname{sd}(n)$ denotes the sum of the digits of $n$.\n",
"\n",
"If `r` is undefined (i.e., if the function is called with only one argument), the function returns `sd(n, r)` for the largest `r` for which it makes sense. In this case, explain your stopping criteria in detail.\n",
"\n",
"Write a `main()` function that will test all the possible inputs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 3.** Write a function `get_digit(n, left, right)`. The function returns\n",
"* the `d`-th digit of `n` from the left if the function was called as `get_digit(n, left=d)`. \n",
" If the required digit doesn't exist, the function must return -1.\n",
"* the `d`-th digit of `n` from the right if the function was called as `get_digit(n, right=d)`. \n",
" If the required digit doesn't exist, the function must return 0.\n",
"* -1 otherwise, i.e., for calls `get_digit(n)` and `get_digit(n, left=d1, right=d2)`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 4.** Write a function `rev(n)` that returns the value of the integer argument `n` with its digits order reversed ($1719 \\mapsto 9171$). The sign of the number must remain unchanged ($-1719 \\mapsto -9171$).\n",
"\n",
"Write a function `main()` that loads a natural number `n` and then `n` integers. The function has to print which of the numbers (first, second, etc.; you can write $k$-th as `number #k`) has the largest reversed value.\n",
"\n",
"**Hint:** We know how to take the last digit $d$ out of a number $x$. However, we can add that digit to the end of a new number $y$ by computing $y = 10y + d$. Doing so for all digits will produce the reversed number."
]
}
],
"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
}