Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pending changes exported from your codespace #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notebook/problems.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells":[{"attachments":{},"cell_type":"markdown","id":"27b09e13","metadata":{},"source":["# Optimization of Algorithms problems"]},{"attachments":{},"cell_type":"markdown","id":"ed05e9bf","metadata":{},"source":["## Exercise 1\n","### Code Optimization for Text Processing\n","\n","You are provided with a text processing code to perform the following operations:\n","\n","1. Convert all text to lowercase.\n","2. Remove punctuation marks.\n","3. Count the frequency of each word.\n","4. Show the 5 most common words.\n","\n","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."]},{"cell_type":"code","execution_count":1,"id":"8467465b","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["'the': 5 times\n","'of': 3 times\n","'in': 2 times\n","'a': 2 times\n","'she': 2 times\n"]}],"source":["import string\n","\n","def process_text(text):\n"," # Text to lowercase\n"," text = text.lower()\n","\n"," # Remove punctuation\n"," for p in string.punctuation:\n"," text = text.replace(p, \"\")\n","\n"," # Split text into words\n"," words = text.split()\n","\n"," # Count frecuencies\n"," frequencies = {}\n"," for w in words:\n"," if w in frequencies:\n"," frequencies[w] += 1\n"," else:\n"," frequencies[w] = 1\n","\n"," sorted_frequencies = sorted(frequencies.items(), key = lambda x: x[1], reverse = True)\n","\n"," # Get 5 most-common words\n"," top_5 = sorted_frequencies[:5]\n"," \n"," for w, frequency in top_5:\n"," print(f\"'{w}': {frequency} times\")\n","\n","text = \"\"\"\n"," In the heart of the city, Emily discovered a quaint little café, hidden away from the bustling streets. \n"," The aroma of freshly baked pastries wafted through the air, drawing in passersby. As she sipped on her latte, \n"," she noticed an old bookshelf filled with classics, creating a cozy atmosphere that made her lose track of time.\n","\"\"\"\n","process_text(text)"]},{"cell_type":"markdown","id":"29040779","metadata":{},"source":["Points to optimize:\n","\n","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","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","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","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."]},{"cell_type":"code","execution_count":2,"id":"57cd6641","metadata":{},"outputs":[],"source":["# TODO"]},{"attachments":{},"cell_type":"markdown","id":"011996bc","metadata":{},"source":["## Exercise 2\n","### Code Optimization for List Processing\n","\n","You have been given a code that performs operations on a list of numbers for:\n","\n","1. Filter out even numbers.\n","2. Duplicate each number.\n","3. Add all numbers.\n","4. Check if the result is a prime number.\n","\n","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."]},{"cell_type":"code","execution_count":3,"id":"783d03a0","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Result: 60, ¿Prime? No\n"]}],"source":["import math\n","\n","def is_prime(n):\n"," if n <= 1:\n"," return False\n"," for i in range(2, int(math.sqrt(n)) + 1):\n"," if n % i == 0:\n"," return False\n"," return True\n","\n","def process_list(list_):\n"," filtered_list = []\n"," for num in list_:\n"," if num % 2 == 0:\n"," filtered_list.append(num)\n"," \n"," duplicate_list = []\n"," for num in filtered_list:\n"," duplicate_list.append(num * 2)\n"," \n"," sum = 0\n"," for num in duplicate_list:\n"," sum += num\n","\n"," prime = is_prime(sum)\n"," \n"," return sum, prime\n","\n","list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n","result, result_prime = process_list(list_)\n","print(f\"Result: {result}, ¿Prime? {'Yes' if result_prime else 'No'}\")"]},{"cell_type":"markdown","id":"128d564e","metadata":{},"source":["Points to optimize:\n","\n","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","2. **Duplication**: The list is traversed multiple times. Is there a way to do this more efficiently?\n","3. **Summing**: The numbers in a list are summed through a loop. Python has built-in functions that can optimize this.\n","4. **Function `is_prime`**: While this function is relatively efficient, investigate if there are ways to make it even faster.\n","5. **Modularity**: Consider breaking the code into smaller functions, each focused on a specific task."]},{"cell_type":"code","execution_count":4,"id":"f40e35d6","metadata":{},"outputs":[],"source":["# TODO"]},{"attachments":{},"cell_type":"markdown","id":"1af70806","metadata":{},"source":["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."]}],"metadata":{"interpreter":{"hash":"9248718ffe6ce6938b217e69dbcc175ea21f4c6b28a317e96c05334edae734bb"},"kernelspec":{"display_name":"Python 3 (ipykernel)","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.11.4"}},"nbformat":4,"nbformat_minor":5}
{"cells":[{"attachments":{},"cell_type":"markdown","id":"27b09e13","metadata":{},"source":["# Optimization of Algorithms problems"]},{"attachments":{},"cell_type":"markdown","id":"ed05e9bf","metadata":{},"source":["## Exercise 1\n","### Code Optimization for Text Processing\n","\n","You are provided with a text processing code to perform the following operations:\n","\n","1. Convert all text to lowercase.\n","2. Remove punctuation marks.\n","3. Count the frequency of each word.\n","4. Show the 5 most common words.\n","\n","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."]},{"cell_type":"code","execution_count":1,"id":"8467465b","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["'the': 5 times\n","'of': 3 times\n","'in': 2 times\n","'a': 2 times\n","'she': 2 times\n"]}],"source":["import string\n","\n","def process_text(text):\n"," # Text to lowercase\n"," text = text.lower()\n","\n"," # Remove punctuation\n"," for p in string.punctuation:\n"," text = text.replace(p, \"\")\n","\n"," # Split text into words\n"," words = text.split()\n","\n"," # Count frecuencies\n"," frequencies = {}\n"," for w in words:\n"," if w in frequencies:\n"," frequencies[w] += 1\n"," else:\n"," frequencies[w] = 1\n","\n"," sorted_frequencies = sorted(frequencies.items(), key = lambda x: x[1], reverse = True)\n","\n"," # Get 5 most-common words\n"," top_5 = sorted_frequencies[:5]\n"," \n"," for w, frequency in top_5:\n"," print(f\"'{w}': {frequency} times\")\n","\n","text = \"\"\"\n"," In the heart of the city, Emily discovered a quaint little café, hidden away from the bustling streets. \n"," The aroma of freshly baked pastries wafted through the air, drawing in passersby. As she sipped on her latte, \n"," she noticed an old bookshelf filled with classics, creating a cozy atmosphere that made her lose track of time.\n","\"\"\"\n","process_text(text)"]},{"cell_type":"markdown","id":"29040779","metadata":{},"source":["Points to optimize:\n","\n","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","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","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","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."]},{"cell_type":"code","execution_count":3,"id":"57cd6641","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["'the': 5 times\n","'of': 3 times\n","'in': 2 times\n","'a': 2 times\n","'she': 2 times\n"]}],"source":["# TODO\n","from collections import Counter\n","import string\n","\n","def count(text):\n"," counted_words = text.split()\n"," return Counter(counted_words)\n","\n","def most_common(frequencies, n = 5):\n"," return frequencies.most_common(n)\n","\n","def process_text(text):\n"," # Text to lowercase\n"," text = text.lower()\n","\n","\n"," # Count frecuencies\n"," frequencies = count(text)\n","\n"," # Get 5 most-common words\n"," top_5 = most_common(frequencies)\n"," \n"," for w, frequency in top_5:\n"," print(f\"'{w}': {frequency} times\")\n","\n","text = \"\"\"\n"," In the heart of the city, Emily discovered a quaint little café, hidden away from the bustling streets. \n"," The aroma of freshly baked pastries wafted through the air, drawing in passersby. As she sipped on her latte, \n"," she noticed an old bookshelf filled with classics, creating a cozy atmosphere that made her lose track of time.\n","\"\"\"\n","process_text(text)"]},{"attachments":{},"cell_type":"markdown","id":"011996bc","metadata":{},"source":["## Exercise 2\n","### Code Optimization for List Processing\n","\n","You have been given a code that performs operations on a list of numbers for:\n","\n","1. Filter out even numbers.\n","2. Duplicate each number.\n","3. Add all numbers.\n","4. Check if the result is a prime number.\n","\n","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."]},{"cell_type":"code","execution_count":3,"id":"783d03a0","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Result: 60, ¿Prime? No\n"]}],"source":["import math\n","\n","def is_prime(n):\n"," if n <= 1:\n"," return False\n"," for i in range(2, int(math.sqrt(n)) + 1):\n"," if n % i == 0:\n"," return False\n"," return True\n","\n","def process_list(list_):\n"," filtered_list = []\n"," for num in list_:\n"," if num % 2 == 0:\n"," filtered_list.append(num)\n"," \n"," duplicate_list = []\n"," for num in filtered_list:\n"," duplicate_list.append(num * 2)\n"," \n"," sum = 0\n"," for num in duplicate_list:\n"," sum += num\n","\n"," prime = is_prime(sum)\n"," \n"," return sum, prime\n","\n","list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n","result, result_prime = process_list(list_)\n","print(f\"Result: {result}, ¿Prime? {'Yes' if result_prime else 'No'}\")"]},{"cell_type":"markdown","id":"128d564e","metadata":{},"source":["Points to optimize:\n","\n","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","2. **Duplication**: The list is traversed multiple times. Is there a way to do this more efficiently?\n","3. **Summing**: The numbers in a list are summed through a loop. Python has built-in functions that can optimize this.\n","4. **Function `is_prime`**: While this function is relatively efficient, investigate if there are ways to make it even faster.\n","5. **Modularity**: Consider breaking the code into smaller functions, each focused on a specific task."]},{"cell_type":"code","execution_count":4,"id":"f40e35d6","metadata":{},"outputs":[],"source":["# TODO\n","\n","def is_prime(n):\n"," if (n <= 1):\n"," return False\n"," if (n <= 3):\n"," return True\n"," if (n % 2 == 0) or (n % 3 == 0):\n"," return False\n"," i = 5\n"," while ((i * i) <= n):\n"," if (n % i == 0) or (n % (i + 2) == 0):\n"," return False\n"," i += 6\n"," return True\n","\n","def filter_duplicate(list_):\n"," # Filter and duplicate numbers in a single step\n"," return [num * 2 for num in list_ if num % 2 == 0]\n","\n","def process_list(list_):\n"," duplicate_list = filter_duplicate(list_)\n"," \n"," # Compute sum\n"," sum_ = sum(duplicate_list)\n"," prime = is_prime(sum_)\n"," \n"," return sum_, prime\n","\n","list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n","result, result_prime = process_list(list_)\n","print(f\"Result: {result}, ¿Prime? {'Yes' if result_prime else 'No'}\")"]},{"attachments":{},"cell_type":"markdown","id":"1af70806","metadata":{},"source":["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."]}],"metadata":{"interpreter":{"hash":"9248718ffe6ce6938b217e69dbcc175ea21f4c6b28a317e96c05334edae734bb"},"kernelspec":{"display_name":"Python 3 (ipykernel)","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.11.4"}},"nbformat":4,"nbformat_minor":5}