Skip to content

Commit 5a85795

Browse files
committed
Re-organized and updates assignment and solution notebooks
1 parent 1c63fd0 commit 5a85795

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

notebooks/assignment.ipynb

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "275f1ad9",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import string\n",
11+
"from collections import Counter"
12+
]
13+
},
14+
{
15+
"attachments": {},
16+
"cell_type": "markdown",
17+
"id": "27b09e13",
18+
"metadata": {},
19+
"source": [
20+
"# Optimization of Algorithms problems"
21+
]
22+
},
23+
{
24+
"attachments": {},
25+
"cell_type": "markdown",
26+
"id": "ed05e9bf",
27+
"metadata": {},
28+
"source": [
29+
"## Exercise 1\n",
30+
"### Code Optimization for Text Processing\n",
31+
"\n",
32+
"You are provided with a text processing code to perform the following operations:\n",
33+
"\n",
34+
"1. Convert all text to lowercase.\n",
35+
"2. Remove punctuation marks.\n",
36+
"3. Count the frequency of each word.\n",
37+
"4. Show the 5 most common words.\n",
38+
"\n",
39+
"The code works, but it is inefficient and can be optimized. Your task is to identify areas that can be improved and rewrite those parts to make the code more efficient and readable."
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"id": "8467465b",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"def remove_punctuation(text):\n",
50+
" translator = str.maketrans(\"\", \"\", string.punctuation)\n",
51+
" return text.translate(translator)\n",
52+
"\n",
53+
"def count_words(text):\n",
54+
" # Split text into words\n",
55+
" palabras = text.split()\n",
56+
"\n",
57+
" return Counter(palabras)\n",
58+
"\n",
59+
"def get_most_common(frequencies, n = 5):\n",
60+
" return frequencies.most_common(n)\n",
61+
"\n",
62+
"def process_text(text):\n",
63+
" # Text to lowercase\n",
64+
" text = text.lower()\n",
65+
"\n",
66+
" # Remove punctuation\n",
67+
" text = remove_punctuation(text)\n",
68+
" \n",
69+
" # Count frequencies\n",
70+
" frequencies = count_words(text)\n",
71+
" \n",
72+
" top_5 = get_most_common(frequencies)\n",
73+
" \n",
74+
" for w, frequency in top_5:\n",
75+
" print(f\"'{w}': {frequency} veces\")\n",
76+
"\n",
77+
"text = \"\"\"\n",
78+
" In the heart of the city, Emily discovered a quaint little café, hidden away from the bustling streets. \n",
79+
" The aroma of freshly baked pastries wafted through the air, drawing in passersby. As she sipped on her latte, \n",
80+
" she noticed an old bookshelf filled with classics, creating a cozy atmosphere that made her lose track of time.\n",
81+
"\"\"\"\n",
82+
"\n",
83+
"process_text(text)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"id": "29040779",
89+
"metadata": {},
90+
"source": [
91+
"Points to optimize:\n",
92+
"\n",
93+
"1. **Removal of punctuation marks**: Using `replace` in a loop can be inefficient, especially with long texts. Look for a more efficient way to remove punctuation marks.\n",
94+
"2. **Frequency count**: The code checks for the existence of each word in the dictionary and then updates its count. This can be done more efficiently with certain data structures in Python.\n",
95+
"3. **Sort and select:** Consider if there is a more direct or efficient way to get the 5 most frequent words without sorting all the words.\n",
96+
"4. **Modularity**: Break the code into smaller functions so that each one performs a specific task. This will not only optimize performance, but also make the code more readable and maintainable."
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"id": "57cd6641",
103+
"metadata": {},
104+
"outputs": [],
105+
"source": [
106+
"# Your code here\n"
107+
]
108+
},
109+
{
110+
"attachments": {},
111+
"cell_type": "markdown",
112+
"id": "011996bc",
113+
"metadata": {},
114+
"source": [
115+
"## Exercise 2\n",
116+
"### Code Optimization for List Processing\n",
117+
"\n",
118+
"You have been given a code that performs operations on a list of numbers for:\n",
119+
"\n",
120+
"1. Filter out even numbers.\n",
121+
"2. Duplicate each number.\n",
122+
"3. Add all numbers.\n",
123+
"4. Check if the result is a prime number.\n",
124+
"\n",
125+
"The code provided achieves its goal, but it may be inefficient. Your task is to identify and improve the parts of the code to increase its efficiency."
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"id": "783d03a0",
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"import math\n",
136+
"\n",
137+
"def is_prime(n):\n",
138+
" if n <= 1:\n",
139+
" return False\n",
140+
" for i in range(2, int(math.sqrt(n)) + 1):\n",
141+
" if n % i == 0:\n",
142+
" return False\n",
143+
" return True\n",
144+
"\n",
145+
"def process_list(list_):\n",
146+
" filtered_list = []\n",
147+
" for num in list_:\n",
148+
" if num % 2 == 0:\n",
149+
" filtered_list.append(num)\n",
150+
" \n",
151+
" duplicate_list = []\n",
152+
" for num in filtered_list:\n",
153+
" duplicate_list.append(num * 2)\n",
154+
" \n",
155+
" sum = 0\n",
156+
" for num in duplicate_list:\n",
157+
" sum += num\n",
158+
"\n",
159+
" prime = is_prime(sum)\n",
160+
" \n",
161+
" return sum, prime\n",
162+
"\n",
163+
"nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
164+
"result, result_prime = process_list(nums)\n",
165+
"print(f\"Result: {result}, ¿Prime? {'Yes' if result_prime else 'No'}\")"
166+
]
167+
},
168+
{
169+
"cell_type": "markdown",
170+
"id": "128d564e",
171+
"metadata": {},
172+
"source": [
173+
"Points to optimize:\n",
174+
"\n",
175+
"1. **Filter numbers**: The code goes through the original list to filter out even numbers. Consider a more efficient way to filter the list.\n",
176+
"2. **Duplication**: The list is traversed multiple times. Is there a way to do this more efficiently?\n",
177+
"3. **Summing**: The numbers in a list are summed through a loop. Python has built-in functions that can optimize this.\n",
178+
"4. **Function `is_prime`**: While this function is relatively efficient, investigate if there are ways to make it even faster.\n",
179+
"5. **Modularity**: Consider breaking the code into smaller functions, each focused on a specific task."
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"id": "f40e35d6",
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"# Your code here\n"
190+
]
191+
},
192+
{
193+
"attachments": {},
194+
"cell_type": "markdown",
195+
"id": "1af70806",
196+
"metadata": {},
197+
"source": [
198+
"Both exercises will help you improve your code performance optimization skills and give you a better understanding of how different data structures and programming techniques can affect the efficiency of your code."
199+
]
200+
}
201+
],
202+
"metadata": {
203+
"kernelspec": {
204+
"display_name": "Python 3",
205+
"language": "python",
206+
"name": "python3"
207+
},
208+
"language_info": {
209+
"codemirror_mode": {
210+
"name": "ipython",
211+
"version": 3
212+
},
213+
"file_extension": ".py",
214+
"mimetype": "text/x-python",
215+
"name": "python",
216+
"nbconvert_exporter": "python",
217+
"pygments_lexer": "ipython3",
218+
"version": "3.11.4"
219+
}
220+
},
221+
"nbformat": 4,
222+
"nbformat_minor": 5
223+
}
File renamed without changes.

0 commit comments

Comments
 (0)