Skip to content

Commit b48e8f4

Browse files
Add files via upload
1 parent a4cbae7 commit b48e8f4

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "SesionThree_Exercises.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"toc_visible": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {
20+
"id": "KKn8pLWhJ0X9"
21+
},
22+
"source": [
23+
"# PYTHON COURSE FOR SCIENTIFIC PROGRAMMING \n",
24+
"**Contributors:** \\\n",
25+
"Artur Llabrés Brustenga: Artur.Llabres@e-campus.uab.cat \\\n",
26+
"Gerard Navarro Pérez: Gerard.NavarroP@e-campus.uab.cat \\\n",
27+
"Arnau Parrilla Gibert: Arnau.Parrilla@e-campus.uab.cat \\\n",
28+
"Jan Scarabelli Calopa:Jan.Scarabelli@e-campus.uab.cat \\\n",
29+
"Xabier Oyanguren Asua: Xabier.Oyanguren@e-campus.uab.cat \\\n",
30+
"\n",
31+
"Course material can be found at: https://llacorp.github.io/Python-Course-for-Scientific-Programming/ "
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {
37+
"id": "eYqb_LTserLh"
38+
},
39+
"source": [
40+
"# LESSON 3 EXERCISES"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {
46+
"id": "-rYMKOqDez5v"
47+
},
48+
"source": [
49+
"## <ins>Exercise 1</ins>:"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {
55+
"id": "0-NaBG9XhgU8"
56+
},
57+
"source": [
58+
"Write a Python function to check whether a number is perfect or not. It should return `True` or `False`.\r\n",
59+
"\r\n",
60+
"***Wikipedia source***: In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself). \r\n",
61+
"<ins>For example</ins>: The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128."
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {
67+
"id": "tR4v1HGeiApl"
68+
},
69+
"source": [
70+
"**SOLUTION**:"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"metadata": {
76+
"colab": {
77+
"base_uri": "https://localhost:8080/"
78+
},
79+
"id": "4Iciqs4Sh-ar",
80+
"outputId": "f423d936-ccad-401d-ef14-e115c40708cb"
81+
},
82+
"source": [
83+
"def perfect_number(n):\r\n",
84+
" sum = 0\r\n",
85+
" for x in range(1, n):\r\n",
86+
" if n % x == 0:\r\n",
87+
" sum += x\r\n",
88+
" return sum == n\r\n",
89+
" \r\n",
90+
"print(perfect_number(6))"
91+
],
92+
"execution_count": null,
93+
"outputs": [
94+
{
95+
"output_type": "stream",
96+
"text": [
97+
"True\n"
98+
],
99+
"name": "stdout"
100+
}
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {
106+
"id": "0IZIsOxNjbhw"
107+
},
108+
"source": [
109+
"## <ins>Exercise 2</ins>:"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {
115+
"id": "PVlUe6mHjpeU"
116+
},
117+
"source": [
118+
"Write two Python functions to find the $F_n$ Fibonacci number using an iterative method and recursion. The only argument is `n`.\r\n",
119+
"\r\n",
120+
"***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,\r\n",
121+
"\r\n",
122+
"$$F_{0}=0,\\quad F_{1}=1,$$\r\n",
123+
"and\r\n",
124+
"$$F_{n}=F_{n-1}+F_{n-2}$$\r\n",
125+
"for $n > 1$. \r\n",
126+
"The beginning of the sequence is thus:\r\n",
127+
"$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...$$\r\n",
128+
"\r\n",
129+
"<ins>For example</ins>: The Fibonacci number $F_7$ equals $13$, so the functions should return this value."
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {
135+
"id": "XKhg1tVkjkto"
136+
},
137+
"source": [
138+
"**SOLUTION**:"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"metadata": {
144+
"id": "pKC9NZDYjnkg"
145+
},
146+
"source": [
147+
"def F_iter(n):\r\n",
148+
" if (n == 0):\r\n",
149+
" return 0\r\n",
150+
" elif (n == 1):\r\n",
151+
" return 1\r\n",
152+
" elif (n > 1):\r\n",
153+
" fn = 0\r\n",
154+
" fn1 = 1\r\n",
155+
" fn2 = 2\r\n",
156+
" for i in range(3, n):\r\n",
157+
" fn = fn1 + fn2\r\n",
158+
" fn1 = fn2\r\n",
159+
" fn2 = fn\r\n",
160+
" return fn\r\n",
161+
"\r\n",
162+
"def F(n):\r\n",
163+
" if (n == 0):\r\n",
164+
" return 0\r\n",
165+
" elif (n == 1):\r\n",
166+
" return 1\r\n",
167+
" elif (n > 1):\r\n",
168+
" return (F(n-1) + F(n-2))"
169+
],
170+
"execution_count": null,
171+
"outputs": []
172+
},
173+
{
174+
"cell_type": "code",
175+
"metadata": {
176+
"colab": {
177+
"base_uri": "https://localhost:8080/"
178+
},
179+
"id": "vmuuU6Qb4tHO",
180+
"outputId": "bc611b30-0594-4b6c-9efb-f6ff2c8507f0"
181+
},
182+
"source": [
183+
"print(F_iter(7))\r\n",
184+
"print(F(7))"
185+
],
186+
"execution_count": null,
187+
"outputs": [
188+
{
189+
"output_type": "stream",
190+
"text": [
191+
"13\n",
192+
"13\n"
193+
],
194+
"name": "stdout"
195+
}
196+
]
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"metadata": {
201+
"id": "bcsyVRrLjeNA"
202+
},
203+
"source": [
204+
"## <ins>Exercise 3</ins>:"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {
210+
"id": "mq5VNFVZjqdm"
211+
},
212+
"source": [
213+
"Write a Python program to create a dictionary from a string.\r\n",
214+
"\r\n",
215+
"***Note***: Track the count of the letters from the string. \r\n",
216+
"<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}`"
217+
]
218+
},
219+
{
220+
"cell_type": "markdown",
221+
"metadata": {
222+
"id": "w0UpFVuBjlXc"
223+
},
224+
"source": [
225+
"**SOLUTION**:"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"metadata": {
231+
"id": "enTtP8kljoZa",
232+
"colab": {
233+
"base_uri": "https://localhost:8080/"
234+
},
235+
"outputId": "3dfbcf73-0805-4853-a8c6-1c5949ea70a3"
236+
},
237+
"source": [
238+
"str1 = 'scn2python' \r\n",
239+
"my_dict = {}\r\n",
240+
"for letter in str1:\r\n",
241+
" if (my_dict.get(letter) == None):\r\n",
242+
" my_dict[letter] = 1\r\n",
243+
" else:\r\n",
244+
" my_dict[letter] += 1\r\n",
245+
"print(my_dict)"
246+
],
247+
"execution_count": null,
248+
"outputs": [
249+
{
250+
"output_type": "stream",
251+
"text": [
252+
"{'s': 1, 'c': 1, 'n': 2, '2': 1, 'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1}\n"
253+
],
254+
"name": "stdout"
255+
}
256+
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {
261+
"id": "ngPDJ-pSjgGb"
262+
},
263+
"source": [
264+
"## <ins>Exercise 4</ins>:"
265+
]
266+
},
267+
{
268+
"cell_type": "markdown",
269+
"metadata": {
270+
"id": "oMSJ_bvjjrNC"
271+
},
272+
"source": [
273+
""
274+
]
275+
},
276+
{
277+
"cell_type": "markdown",
278+
"metadata": {
279+
"id": "JtjyQi2ujlzw"
280+
},
281+
"source": [
282+
"**SOLUTION**:"
283+
]
284+
},
285+
{
286+
"cell_type": "code",
287+
"metadata": {
288+
"id": "ByVIxIpPjo32"
289+
},
290+
"source": [
291+
""
292+
],
293+
"execution_count": null,
294+
"outputs": []
295+
}
296+
]
297+
}

0 commit comments

Comments
 (0)