You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{"cells":[{"cell_type":"markdown","source":["# PYTHON COURSE FOR SCIENTIFIC PROGRAMMING \n","**Contributors:** \\\n","Artur Llabrés Brustenga: Artur.Llabres@e-campus.uab.cat \\\n","Gerard Navarro Pérez: Gerard.NavarroP@e-campus.uab.cat \\\n","Arnau Parrilla Gibert: Arnau.Parrilla@e-campus.uab.cat \\\n","Jan Scarabelli Calopa:Jan.Scarabelli@e-campus.uab.cat \\\n","Xabier Oyanguren Asua: Xabier.Oyanguren@e-campus.uab.cat\n","\n","Course material can be found at: https://llacorp.github.io/Python-Course-for-Scientific-Programming/ "],"metadata":{"id":"P4hDyWJnlkkV"}},{"cell_type":"markdown","metadata":{"id":"-rYMKOqDez5v"},"source":["## <ins>Exercise 1</ins>:"]},{"cell_type":"markdown","metadata":{"id":"0-NaBG9XhgU8"},"source":["Write a Python function to check whether a number is perfect or not. It should return `True` or `False`.\n","\n","***Wikipedia source***: \"*A perfect number is a positive integer that is equal to the sum of its positive divisors excluding the number itself.*\n","\n","*Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).*\" \n","\n","<ins>For example</ins>: \n","\n","The first perfect number is 6, because 1, 2, and 3 are its positive divisors excluding itself, and 1 + 2 + 3 = 6.\n","\n","Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6.\n","\n","\n","The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128."]},{"cell_type":"markdown","metadata":{"id":"tR4v1HGeiApl"},"source":["**SOLUTION**:"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"4Iciqs4Sh-ar","outputId":"f423d936-ccad-401d-ef14-e115c40708cb"},"outputs":[{"name":"stdout","output_type":"stream","text":["True\n"]}],"source":["def perfect_number(n):\n"," sum = 0\n"," for x in range(1, n):\n"," if n % x == 0:\n"," sum += x\n"," return sum == n\n"," \n","print(perfect_number(6))"]},{"cell_type":"markdown","metadata":{"id":"0IZIsOxNjbhw"},"source":["## <ins>Exercise 2</ins>:"]},{"cell_type":"markdown","metadata":{"id":"PVlUe6mHjpeU"},"source":["Write two Python functions to find the $F_n$ Fibonacci number, one using an iterative method and the other one using recursion. The only argument is `n` and the $n$-th number in the Fibonacci sequence should be retuned.\n","\n","***Wikipedia source***: In mathematics, the Fibonacci numbers, commonly denoted $F_n$, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,\n","\n","$$F_{0}=0,\\quad F_{1}=1,$$\n","and\n","$$F_{n}=F_{n-1}+F_{n-2}$$\n","for $n > 1$. \n","The beginning of the sequence is thus:\n","$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...$$\n","\n","<ins>For example</ins>: The Fibonacci number $F_7$ equals $13$, so the functions should return this value."]},{"cell_type":"markdown","metadata":{"id":"XKhg1tVkjkto"},"source":["**SOLUTION**:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pKC9NZDYjnkg"},"outputs":[],"source":["def F_iter(n):\n"," if (n == 0):\n"," return 0\n"," elif (n == 1):\n"," return 1\n"," elif (n > 1):\n"," fn = 0\n"," fn1 = 1\n"," fn2 = 2\n"," for i in range(3, n):\n"," fn = fn1 + fn2\n"," fn1 = fn2\n"," fn2 = fn\n"," return fn\n","\n","def F(n):\n"," if (n == 0):\n"," return 0\n"," elif (n == 1):\n"," return 1\n"," elif (n > 1):\n"," return (F(n-1) + F(n-2))"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"vmuuU6Qb4tHO","outputId":"bc611b30-0594-4b6c-9efb-f6ff2c8507f0"},"outputs":[{"name":"stdout","output_type":"stream","text":["13\n","13\n"]}],"source":["print(F_iter(7))\n","print(F(7))"]},{"cell_type":"markdown","metadata":{"id":"bcsyVRrLjeNA"},"source":["## <ins>Exercise 3</ins>:"]},{"cell_type":"markdown","metadata":{"id":"mq5VNFVZjqdm"},"source":["Write a Python program to create a dictionary from a string. The dictionary should contain as keys the characters of the string and as values the number of times the character is repeated.\n","\n","***Note 1***: Track the count of the letters from the string. \n","***Note 2***: To check if a character is in a string just type `\"char\" in str1` \n","<ins>For example</ins>: The string 'scn2python' should output: `{'s': 1, 'c': 1, 'n': 2, '2': 1, 'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1}`"]},{"cell_type":"markdown","metadata":{"id":"w0UpFVuBjlXc"},"source":["**SOLUTION**:"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"enTtP8kljoZa","outputId":"3dfbcf73-0805-4853-a8c6-1c5949ea70a3"},"outputs":[{"name":"stdout","output_type":"stream","text":["{'s': 1, 'c': 1, 'n': 2, '2': 1, 'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1}\n"]}],"source":["str1 = 'scn2python' \n","my_dict = {}\n","for letter in str1:\n"," if (my_dict.get(letter) == None): #or letter not in my_dict.keys()\n"," my_dict[letter] = 1\n"," else:\n"," my_dict[letter] += 1\n","print(my_dict)"]},{"cell_type":"markdown","metadata":{"id":"ngPDJ-pSjgGb"},"source":["## <ins>Exercise 4</ins>:"]},{"cell_type":"markdown","metadata":{"id":"oMSJ_bvjjrNC"},"source":["Write a Python program to perform numerical integration using rectangles.\n","\n","To test the program use the function\n","\n","$$f(x)=\\frac{x^4(1-x^4)}{1+x^2}dx$$\n","\n","and integrate it between $1$ and $2$. The result should be about $0.522...$ for $n=10000$.\n","\n","***Note 1***: Remember that we can approximate an integral using tiny rectangles given a step $\\Delta x$ with\n","$$\\int_{a}^{b} f(x)dx = \\sum_{i=0}^{n} \\Delta xf(a+i\\Delta x)$$\n","\n","We will define our $\\Delta x=\\frac{b-a}{n}$. For higher values of $n$ (*i.e.* smaller $\\Delta x$), the approximation is more precise\n","\n","<img src=\"https://i.stack.imgur.com/BJPUl.png\" alt=\"Drawing\" style=\"width: 500px;\"/>\n","\n","Define two functions, one for $f(x)$ and the other one must return the integral solution given $a$, $b$ and $n$.\n","\n","***Note 2***: You can call a function inside another function."]},{"cell_type":"markdown","metadata":{"id":"JtjyQi2ujlzw"},"source":["**SOLUTION**:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ByVIxIpPjo32","outputId":"e2199d33-3fdd-4b3f-ea8b-b8dbe43f82d8"},"outputs":[{"name":"stdout","output_type":"stream","text":["0.5226816058039061\n"]}],"source":["#the function to be integrated:\n","def f(x):\n"," return x ** 4 * (1 - x) ** 4 / (1 + x ** 2)\n"," \n","#define a function to do integration of f(x) btw. 0 and 1:\n","def trapezium(a, b, n):\n"," h = (b-a) / n\n"," intgr = 0\n"," for i in range(0, n+1):\n"," intgr = intgr + h * f(a + i * h)\n"," return intgr\n"," \n","print(trapezium(1, 2, 10000))"]}],"metadata":{"colab":{"collapsed_sections":[],"name":"Exercises_Lecture_03.ipynb","provenance":[],"toc_visible":true},"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.8.5"}},"nbformat":4,"nbformat_minor":0}
1
+
{
2
+
"cells": [
3
+
{
4
+
"cell_type": "markdown",
5
+
"metadata": {
6
+
"id": "P4hDyWJnlkkV"
7
+
},
8
+
"source": [
9
+
"# Lecture III: EXERCISES\n",
10
+
"\n",
11
+
"----"
12
+
]
13
+
},
14
+
{
15
+
"cell_type": "markdown",
16
+
"metadata": {
17
+
"id": "-rYMKOqDez5v"
18
+
},
19
+
"source": [
20
+
"## <ins>Exercise 1</ins>:"
21
+
]
22
+
},
23
+
{
24
+
"cell_type": "markdown",
25
+
"metadata": {
26
+
"id": "0-NaBG9XhgU8"
27
+
},
28
+
"source": [
29
+
"Write a Python function to check whether a number is perfect or not. It should return `True` or `False`.\n",
30
+
"\n",
31
+
"***Wikipedia source***: \"*A perfect number is a positive integer that is equal to the sum of its positive divisors excluding the number itself.*\n",
32
+
"\n",
33
+
"*Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).*\"\n",
34
+
"\n",
35
+
"<ins>For example</ins>: \n",
36
+
"\n",
37
+
"The first perfect number is 6, because 1, 2, and 3 are its positive divisors excluding itself, and 1 + 2 + 3 = 6.\n",
38
+
"\n",
39
+
"Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6.\n",
40
+
"\n",
41
+
"\n",
42
+
"The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128."
"Write two Python functions to find the $F_n$ Fibonacci number, one using an iterative method and the other one using recursion. The only argument is `n` and the $n$-th number in the Fibonacci sequence should be retuned.\n",
100
+
"\n",
101
+
"***Wikipedia source***: In mathematics, the Fibonacci numbers, commonly denoted $F_n$, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,\n",
102
+
"\n",
103
+
"$$F_{0}=0,\\quad F_{1}=1,$$\n",
104
+
"and\n",
105
+
"$$F_{n}=F_{n-1}+F_{n-2}$$\n",
106
+
"for $n > 1$. \n",
107
+
"The beginning of the sequence is thus:\n",
108
+
"$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...$$\n",
109
+
"\n",
110
+
"<ins>For example</ins>: The Fibonacci number $F_7$ equals $13$, so the functions should return this value."
"Write a Python program to create a dictionary from a string. The dictionary should contain as keys the characters of the string and as values the number of times the character is repeated.\n",
195
+
"\n",
196
+
"***Note 1***: Track the count of the letters from the string. \n",
197
+
"***Note 2***: To check if a character is in a string just type `\"char\" in str1` \n",
0 commit comments