From a6821cc063d3a7d5b1ba949f35f33e3b2028587e Mon Sep 17 00:00:00 2001 From: gabrieleb76-bot Date: Thu, 7 May 2026 12:25:38 +0100 Subject: [PATCH] updated --- functions.py | 36 ++++++ lab-python-functions.ipynb | 225 ++++++++++++++++++++++++++++++++++--- 2 files changed, 244 insertions(+), 17 deletions(-) create mode 100644 functions.py diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..2eb0343 --- /dev/null +++ b/functions.py @@ -0,0 +1,36 @@ +import string as str_module + +def get_unique_list_f(lst): + unique_list = [] + for item in lst: + if item not in unique_list: + unique_list.append(item) + return unique_list + +def count_case_f(string_input): + uppercase_count = 0 + lowercase_count = 0 + for char in string_input: + if char.isupper(): + uppercase_count += 1 + elif char.islower(): + lowercase_count += 1 + return uppercase_count, lowercase_count + +def remove_punctuation_f(sentence): + clean_sentence = "" + for char in sentence: + if char not in str_module.punctuation: + clean_sentence += char + return clean_sentence + +def word_count_f(sentence): + clean_sentence = remove_punctuation_f(sentence) + words = clean_sentence.split() + return len(words) + + +print(get_unique_list_f([1, 2, 3, 3, 3, 4, 5])) # [1, 2, 3, 4, 5] +print(count_case_f("Hello World")) # (2, 8) +print(remove_punctuation_f("Hello, World!!!")) # Hello World +print(word_count_f("Note : this is an example !!! Good day :)")) # 7 \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..5add794 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -143,9 +143,63 @@ "execution_count": null, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Hello World\n", + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "import string\n", + "\n", + "# Challenge 1 — funciones optimizadas\n", + "\n", + "def get_unique_list(lst):\n", + " \"\"\"\n", + " Toma una lista y retorna una nueva lista con elementos únicos,\n", + " preservando el orden de aparición.\n", + " \"\"\"\n", + " return list(dict.fromkeys(lst))\n", + "\n", + "\n", + "def count_case(string):\n", + " \"\"\"\n", + " Retorna una tupla con la cantidad de letras mayúsculas y minúsculas\n", + " en el string dado.\n", + " \"\"\"\n", + " upper = sum(c.isupper() for c in string)\n", + " lower = sum(c.islower() for c in string)\n", + " return upper, lower\n", + "\n", + "\n", + "def remove_punctuation(sentence):\n", + " \"\"\"\n", + " Elimina todos los signos de puntuación de un string.\n", + " \"\"\"\n", + " return sentence.translate(\n", + " str.maketrans(\"\", \"\", string.punctuation)\n", + " )\n", + "\n", + "\n", + "def word_count(sentence):\n", + " \"\"\"\n", + " Cuenta las palabras en un string, ignorando signos de puntuación.\n", + " Un espacio separa las palabras; no hay espacios al inicio ni al final.\n", + " \"\"\"\n", + " return len(remove_punctuation(sentence).split())\n", + "\n", + "\n", + "\n", + "print(get_unique_list([1, 2, 3, 3, 3, 4, 5])) # [1, 2, 3, 4, 5]\n", + "print(count_case(\"Hello World\")) # (2, 8)\n", + "print(remove_punctuation(\"Hello, World!!!\")) # Hello World\n", + "print(word_count(\"Note : this is an example !!! Good day :)\")) # 7" ] }, { @@ -168,12 +222,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], "source": [ - "# your code goes here" + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def subtract(a, b):\n", + " return a - b\n", + "\n", + "\n", + "def multiply(a, b):\n", + " return a * b\n", + "\n", + "\n", + "def divide(a, b):\n", + " if b == 0:\n", + " return \"Error: Cannot divide by zero\"\n", + " return a / b\n", + "\n", + "\n", + "def calculate(a, b, operator):\n", + " if operator == \"+\":\n", + " return add(a, b)\n", + " elif operator == \"-\":\n", + " return subtract(a, b)\n", + " elif operator == \"*\":\n", + " return multiply(a, b)\n", + " elif operator == \"/\":\n", + " return divide(a, b)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "\n", + "print(calculate(10, 5, \"+\"))\n", + "print(calculate(10, 5, \"-\"))\n", + "print(calculate(10, 5, \"*\"))\n", + "print(calculate(10, 5, \"/\"))" ] }, { @@ -192,12 +291,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "12\n", + "24\n" + ] + } + ], "source": [ - "# your code goes here" + "def add_many(*args):\n", + " return sum(args)\n", + "\n", + "\n", + "def subtract_many(*args):\n", + " result = args[0]\n", + "\n", + " for number in args[1:]:\n", + " result -= number\n", + "\n", + " return result\n", + "\n", + "\n", + "def multiply_many(*args):\n", + " result = 1\n", + "\n", + " for number in args:\n", + " result *= number\n", + "\n", + " return result\n", + "\n", + "\n", + "print(add_many(1, 2, 3, 4))\n", + "print(subtract_many(20, 5, 3))\n", + "print(multiply_many(2, 3, 4))" ] }, { @@ -273,16 +406,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Hello World\n", + "7\n", + "[1, 2, 3, 4]\n", + "(2, 8)\n", + "Hello World\n", + "7\n" + ] + } + ], "source": [ - "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", - "%autoreload 2 \n", - "\n", - "# your code goes here" + "%autoreload 2\n", + "\n", + "from functions import (\n", + " get_unique_list_f,\n", + " count_case_f,\n", + " remove_punctuation_f,\n", + " word_count_f\n", + ")\n", + "\n", + "print(get_unique_list_f([1, 2, 2, 3, 4, 4]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Hello, World!!!\"))\n", + "print(word_count_f(\"Note : this is an example !!! Good day :)\"))" ] }, { @@ -318,15 +475,49 @@ "execution_count": null, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], + "source": [ + "def fibonacci(n):\n", + " if n <= 1:\n", + " return n\n", + "\n", + " return fibonacci(n - 1) + fibonacci(n - 2)\n", + "\n", + "\n", + "def fibonacci_sequence(n):\n", + " sequence = []\n", + "\n", + " for i in range(1, n + 1):\n", + " sequence.append(fibonacci(i))\n", + "\n", + " return sequence\n", + "\n", + "\n", + "print(\"Fibonacci sequence:\", fibonacci_sequence(" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7917b865", + "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "." ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +531,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,