{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 5 - Additional Exercises Solution\n",
"\n",
"## Functions\n",
"\n",
"This notebook contains a few exercises which serve as a repetition of different aspects of functions:\n",
"\n",
"- definition of functions\n",
"- input parameters\n",
"- return values\n",
"- functions called by other functions\n",
"- default parameters.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1: The Answer to the Ultimate Question in Live\n",
"Many computer scientists love the novel *The Hitchhiker's Guide to the Galaxy* by *Douglas Adams* and especially the [The Answer to the thr Ultimate Question in Life, the Universe and Everything](https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#The_Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything_is_42) and even more the both genius and absurd answer 42. If you know about this famous 42, you will see it again and again in computer literature, programming examples or test input.\n",
"\n",
"To respect this tradition, the first exercise is quite simple: Define a function called `the_answer_to_the_ultimate_question_of_life_the_universe_and_everything()` which does not take any input parameters and does not return any value. Instead, it simply prints out the famous number 42. Don't forget to execute this function by calling it.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2: Sum up Even Numbers\n",
"Define a function `sum_even_numbers()`, that sums up the first *n* even numbers and prints the result. The function has a single parameter *n*. If `n = 3`, then the result should be `2 + 4 + 6 = 12`. Call the function with various inputs. What is the result for `n = 6`?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3: Pronic Numbers\n",
"\n",
"A [Pronic Number](https://en.wikipedia.org/wiki/Pronic_number) is a number, which is a product of two consecutive integers. For example, 20 is a pronic number because `20 = 4 * 5` and 4 and 5 are consecutive numbers. Implement a function `ispronic()` which gets an integer as a parameter and checks, if the parameter is pronic. The return value of the `ispronic()` is either `True` or `False`.\n",
"\n",
"Hint: A very easy way to check, if a number `n` is pronic is to go through all numbers from `1` to `n`and check, if the product of the number times it's successor is equal to `n`. \n",
"A more efficient way to implement the function could be to:\n",
"- ake the square root of the parameter\n",
"- round the result to the next integer\n",
"- multiply this result with it's successor\n",
"- check, if the result is equal to the input. \n",
"\n",
"Rounding can be done by casting the square root to integer.\n",
"\n",
"For all numbers from `1`to `100` check if they are pronic. Check, if `42` is pronic."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4: Abundant Numbers\n",
"An [Abundant Number](https://en.wikipedia.org/wiki/Abundant_number) is a number, for which the sum of all proper divisors is greater than the number itself. For example, the number `12` is abundant as the sum of its proper divisors, `1, 2, 3, 4, 6`, is `16` and `16` is greater than `12`.\n",
"Implement a function `isabundant()` which takes an integer as input and returns either `True` if the input is abundant and `False` if not.\n",
"\n",
"For all numbers from `1` to `100` check, if the number is abundant. Then check, which numbers from `1` to `1000` are both pronic and abundant."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 5: Narcisstic Numbers\n",
"A number is a [Narcisstic Number](https://en.wikipedia.org/wiki/Narcissistic_number) if it is the sum of its own digits each raised to the power of the number of digits. Sounds complicated? Let's give an example: `153` is narcissitic. `153` consists of `3` digits. Thus, our exponent is `3`. If we take all digits of `153` by the power of `3` and sum up the results, then we end up with 153. `1³ + 5³ + 3³ = 1 + 125 + 27 = 153`.\n",
"Implement a function `isnarcisstic()` that checks, if a given input is narcissitic. The function is built upon two other functions. \n",
"\n",
"`number_of_digits()` gets an integer as input and returns the number of digits, this integer consists of. Example: `number_of_digits(153)` returns `3`. \n",
"\n",
"`digits()` gets an integer and returns a list of the digits, this integer is composed of.\n",
"Example: `digits(153)` returns `[1, 5, 3]` \n",
"\n",
"Implement these three functions. Check, which of the numbers from 1 to 1000 is narcisstic. Check if 42 is narcisstic."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`42` is pronic, abundant but not narcissitic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 6: Pascal's Triangle\n",
"[Pascal's Triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle) is a triangular shaped array which is often used in number theory and combinatorics. The first line of the triangular consists of one number, the second line consists of two numbers and so on. The numbers are constructed in the following way: The top number and the numbers on the left and right of each row are all `1`s. All other numbers are the sum of the two numbers next to it in the upper row. The following animated figure (linked from Wikipedia) illustrates the construction: ![Pascal's Triangle as published on Wikipedia](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)\n",
"\n",
"Implement a function which takes the integer `n` as input and prints out the first `n` lines of Pascal's triangle. The print can be simplified e.g. in the following way:\n",
"\n",
" How many lines of Pascal`s Triangle shall be printed? 4\n",
" [1]\n",
" [1, 1]\n",
" [1, 2, 1]\n",
" [1, 3, 3, 1]\n",
"\n",
"As you can see, you can simply collect the numbers of each row in a list and print the entire list.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 7: Convert Temperature\n",
"Temperarture is typically measured in Celsius, Fahrenheit or Kelvin. The different measurements can be converted using the following forumulas (C = Celsius, F = Fahrenheit, K = Kelvin)\n",
"\n",
" C = (F - 32) * 5 / 9\n",
" F = C * 9 / 5 + 32\n",
" K = (F + 459.67) * 5 / 9\n",
" F = K * 9 / 5 - 459.67\n",
" K = C + 273.15 \t\n",
" C = K - 273.15\n",
" \n",
"Implement a function, which gets two parameters as input: First the temperature and second the mode. The converted temperature is the return value of the function. The mode can be as follows:\n",
"\n",
" F2K: Fahrenheit to Kelvin\n",
" F2C: Fahrenheit to Celsius\n",
" K2F: Kelvin to Fahrenheit\n",
" K2C: Kelvin to Celsius\n",
" C2F: Celsius to Fahrenheit\n",
" C2K: Celsius to Kelvin\n",
" \n",
"The mode is of data type string. `F2K` means, the input parameter is taken as Fahrenheit and converted into Kelvin according to the above formula. The other modes should be interpreted accordingly. Important: `mode` has a default value `C2F`. Thus, if no mode is given as argument, the input temperature is Celsius and the return value shall be Fahrenheit.\n",
"\n",
"Implement the function and call the function with different arguments."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}