From 33931bb7693f4975062297be5d08a46a5eab5c40 Mon Sep 17 00:00:00 2001 From: MonicaFernandezM Date: Thu, 15 Jan 2026 14:59:29 +0100 Subject: [PATCH 1/2] Updated lab functions extra --- __pycache__/functions.cpython-310.pyc | Bin 0 -> 1059 bytes functions.py | 38 +++ lab-python-functions.ipynb | 436 ++++++++++++++++++++++++-- 3 files changed, 456 insertions(+), 18 deletions(-) create mode 100644 __pycache__/functions.cpython-310.pyc create mode 100644 functions.py diff --git a/__pycache__/functions.cpython-310.pyc b/__pycache__/functions.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..22edd88fd52b9a7c96cfc4d6372c87b30c911e04 GIT binary patch literal 1059 zcma)4!H&}~5Ve!EP19Cd5Moza4!#z)o86ueLI|mF;L<%5E?g=%cDE6!Q`ycgt;%Wn znSKC&!58Gpi8HsJm@y4hfLI)PCNpEtJinQ|(}@VI2cKu@C!dfnXzVW!jOVb;2?#+1 z{Yc&t;Rzo!6@jRM_C#GYK>H#TP0)deMx@(H@NTsrH$i&<+q6Nf$(kOL58m1vkqsSg z$i^=NRm{(w?3Z;dO_5oRmz>yM#BYq$CZ6XiO?dnwzgAh!g^5RU&J{n82N^fA*N-zk z=`EIamaE>hP>D@*WqR_x)ja-bpDvbV#Dz#PI-X*cZvlvq22_Xeb36K@9ePbGh#Ft{ z3$bp1S$0e@fzl4fc#1CFM1j2DUgX~Y6*-s#SLq@HViNZtr1(dj)<5eUL3B+8edp=A zfL35zj(0V?{_rb=$?EW>*13MdM0%APNY5rqwvym-&H3A-qw45*dIvHXWE81&^mf2V z@K&|(ANK&ZnSoe)4pGxOJU58T6CPl^Ybhe!d3^93TW`-3faet6Qv=<#zNp_&3S&DW zHmFX4w^4b!;c!bIZE3Hh54SYd$lB`h>zQOmCb<%f>&s#;m1U`6XD7_+CF7SoRqQ&o zGsabJXHqj5+lkbttAkhSif5@{7YnJm&2_itX36zhVLH5fq z-(C8BfLaO9k|N_)j;GizgCe96kXh}}5gU1JPv3{&@Q=E#po{HgcinR{8a|!pqR8Yk P9A$%(2^nknwHx07K)lg# literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..474c80e --- /dev/null +++ b/functions.py @@ -0,0 +1,38 @@ +#Sum function +def addition_f(num1, num2): + result = num1 + num2 + return result + +#Subtraction function +def subtraction_f(num1, num2): + result = num1 - num2 + return result + +#Multiplication function +def multiplicacion_f(num1, num2): + result = num1 * num2 + return result + +#Division function +def division_f(num1, num2): + if num2 == 0: + return "Error: division by zero" + result = num1 / num2 + return result + +#funcion calculator +def calculate_f(arg1, arg2, operator): + if operator == "+": + return addition_f(arg1, arg2) + elif operator == "-": + return subtraction_f(arg1, arg2) + elif operator == "*": + return multiplicacion_f(arg1, arg2) + elif operator == "/": + if arg2 == 0: + print("The second argument is 0, try again with another number.") + return None + else: + return division_f(arg1, arg2) + else: + print("Invalid Operator") \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..c6aa3e4 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -43,7 +43,29 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " # your code goes here\n", + " elements_unique = list(set(lst))\n", + " return elements_unique\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6085c18b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "my_list = [1,2,3,3,3,3,4,5]\n", + "get_unique = get_unique_list(my_list)\n", + "print(get_unique)" ] }, { @@ -60,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -75,7 +97,37 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " upper_case = 0\n", + " lower_case = 0\n", + " for letra in string:\n", + " if letra.isupper():\n", + " upper_case += 1\n", + " elif letra == ' ':\n", + " pass\n", + " else:\n", + " lower_case += 1\n", + " print(f\"Uppercase count: {upper_case}, Lowercase count: {lower_case}\")\n", + " return " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9749a83d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2, Lowercase count: 8\n" + ] + } + ], + "source": [ + "input_string = \"Hello World\"\n", + "funcion_count_case = count_case(input_string)" ] }, { @@ -92,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -110,6 +162,12 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " result = \"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result+= char\n", + " \n", + " return result\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +180,11 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + "\n", + " clean = remove_punctuation(sentence)\n", + " words = clean.split()\n", + " return len(words)" ] }, { @@ -140,12 +202,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "word_count(\"Note : this is an example !!! Good day : )\")\n" ] }, { @@ -168,12 +242,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(num1, num2):\n", + " result = num1 + num2\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70636610", + "metadata": {}, + "outputs": [], + "source": [ + "def subtraction(num1, num2):\n", + " result = num1 - num2\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "abf91edb", + "metadata": {}, + "outputs": [], + "source": [ + "def multiplicacion(num1, num2):\n", + " result = num1 * num2\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "ee76660a", + "metadata": {}, + "outputs": [], + "source": [ + "def division(num1, num2):\n", + " if num2 == 0:\n", + " return \"Error: division by zero\"\n", + " result = num1 / num2\n", + " return result " + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "3b7719be", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate(arg1, arg2, operator):\n", + " if operator == \"+\":\n", + " return addition(arg1, arg2)\n", + " elif operator == \"-\":\n", + " return subtraction(arg1, arg2)\n", + " elif operator == \"*\":\n", + " return multiplicacion(arg1, arg2) \n", + " elif operator == \"/\":\n", + " if arg2 == 0:\n", + " print(\"The second argument is 0, try again with another number.\")\n", + " return None\n", + " else:\n", + " return division(arg1, arg2)\n", + " else: \n", + " print(\"Invalid Operator\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "ff528323", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The second argument is 0, try again with another number.\n", + "None\n" + ] + } + ], + "source": [ + "calcular = calculate(2,0,\"/\")\n", + "print(calcular)" ] }, { @@ -192,12 +350,108 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", + "execution_count": 46, + "id": "976136db", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(*args):\n", + " result = 0\n", + " for num in args:\n", + " result += num \n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "a2d5a1fe", + "metadata": {}, + "outputs": [], + "source": [ + "def subtraction(*args):\n", + " if not args:\n", + " return 0\n", + " result = args[0] #tomamos el primer numero como base\n", + " for num in args[1:]: #le restamos todos los numeros siguiente\n", + " result -= num\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "8d7f5496", + "metadata": {}, + "outputs": [], + "source": [ + "def multiplicacion(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "5ca064e7", + "metadata": {}, + "outputs": [], + "source": [ + "def division(*args):\n", + " if not args:\n", + " return None\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " if num == 0:\n", + " print(\"Error: division by zero\")\n", + " return None\n", + " result /= num\n", + " return result " + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "ccaefe41", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate(operator, *args):\n", + " if operator == \"+\":\n", + " return addition(*args)\n", + " elif operator == \"-\":\n", + " return subtraction(*args)\n", + " elif operator == \"*\":\n", + " return multiplicacion(*args) \n", + " elif operator == \"/\":\n", + " if args == 0:\n", + " print(\"The second argument is 0, try again with another number.\")\n", + " return None\n", + " else:\n", + " return division(*args)\n", + " else: \n", + " print(\"Invalid Operator\")" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "d2e61400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: division by zero\n" + ] + } + ], + "source": [ + "calculate(\"/\", 100, 2, 5)\n", + "calculate(\"/\", 10, 0, 2)" ] }, { @@ -271,9 +525,97 @@ "You can read more about this here: https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html" ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "0b2ea4e1", + "metadata": {}, + "outputs": [], + "source": [ + "from functions import addition_f, subtraction_f, multiplicacion_f, division_f" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e5300864", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n" + ] + } + ], + "source": [ + "print(addition_f(2,6))" + ] + }, { "cell_type": "code", "execution_count": null, + "id": "8cf3a9a4", + "metadata": {}, + "outputs": [], + "source": [ + "import functions" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "3f48f134", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "5.0\n" + ] + } + ], + "source": [ + "print(functions.addition_f(5,2))\n", + "print(functions.division_f(10,2))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "333f0083", + "metadata": {}, + "outputs": [], + "source": [ + "from functions import addition_f as addf, subtraction_f as subf" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9b2004df", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "8\n" + ] + } + ], + "source": [ + "print(addf(5,2))\n", + "print(subf(10,2))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, "outputs": [], @@ -315,18 +657,76 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def fibonacci_number(n):\n", + "\n", + " if n < 0:\n", + " raise ValueError(\"n must be a non-negative integer\")\n", + " if n == 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " else:\n", + " return fibonacci_number(n-1) + fibonacci_number(n-2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "52147e70", + "metadata": {}, + "outputs": [], + "source": [ + "def fibonacci_sequence(n):\n", + "\n", + " if n < 0:\n", + " raise ValueError(\"n must be a non-negative integer\")\n", + " \n", + " sequence = []\n", + " for i in range(n+1):\n", + " sequence.append(fibonacci_number(i))\n", + " return sequence" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb9f3bcf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n", + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n", + "[0, 1, 1, 2, 3, 5]\n" + ] + } + ], + "source": [ + "print(fibonacci_number(10)) # 55\n", + "print(fibonacci_sequence(10)) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n", + "\n", + "print(fibonacci_sequence(5)) # [0, 1, 1, 2, 3, 5]" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b60025f3", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +740,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.2" } }, "nbformat": 4, From dae00d612f840d6d0d02161d63e8f6a9b7c35109 Mon Sep 17 00:00:00 2001 From: MonicaFernandezM Date: Thu, 15 Jan 2026 17:03:20 +0100 Subject: [PATCH 2/2] Updated feedback --- functions.py | 16 +++++++------- lab-python-functions.ipynb | 44 +++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/functions.py b/functions.py index 474c80e..dc89ec2 100644 --- a/functions.py +++ b/functions.py @@ -1,20 +1,20 @@ #Sum function -def addition_f(num1, num2): +def get_unique_list_f(num1, num2): result = num1 + num2 return result #Subtraction function -def subtraction_f(num1, num2): +def count_case_f(num1, num2): result = num1 - num2 return result #Multiplication function -def multiplicacion_f(num1, num2): +def remove_punctuation_f(num1, num2): result = num1 * num2 return result #Division function -def division_f(num1, num2): +def word_count_f(num1, num2): if num2 == 0: return "Error: division by zero" result = num1 / num2 @@ -23,16 +23,16 @@ def division_f(num1, num2): #funcion calculator def calculate_f(arg1, arg2, operator): if operator == "+": - return addition_f(arg1, arg2) + return get_unique_list_f(arg1, arg2) elif operator == "-": - return subtraction_f(arg1, arg2) + return count_case_f(arg1, arg2) elif operator == "*": - return multiplicacion_f(arg1, arg2) + return remove_punctuation_f(arg1, arg2) elif operator == "/": if arg2 == 0: print("The second argument is 0, try again with another number.") return None else: - return division_f(arg1, arg2) + return word_count_f(arg1, arg2) else: print("Invalid Operator") \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index c6aa3e4..f1a236b 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "id": "6085c18b", "metadata": {}, "outputs": [ @@ -107,13 +107,12 @@ " pass\n", " else:\n", " lower_case += 1\n", - " print(f\"Uppercase count: {upper_case}, Lowercase count: {lower_case}\")\n", - " return " + " return (upper_case, lower_case)" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "id": "9749a83d", "metadata": {}, "outputs": [ @@ -121,13 +120,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "Uppercase count: 2, Lowercase count: 8\n" + "(2, 8)\n" ] } ], "source": [ "input_string = \"Hello World\"\n", - "funcion_count_case = count_case(input_string)" + "funcion_count_case = count_case(input_string)\n", + "print(funcion_count_case)" ] }, { @@ -527,17 +527,17 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "0b2ea4e1", "metadata": {}, "outputs": [], "source": [ - "from functions import addition_f, subtraction_f, multiplicacion_f, division_f" + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "e5300864", "metadata": {}, "outputs": [ @@ -550,7 +550,7 @@ } ], "source": [ - "print(addition_f(2,6))" + "print(get_unique_list_f(2,6))" ] }, { @@ -565,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "3f48f134", "metadata": {}, "outputs": [ @@ -579,18 +579,18 @@ } ], "source": [ - "print(functions.addition_f(5,2))\n", - "print(functions.division_f(10,2))" + "print(functions.get_unique_list_f(5,2))\n", + "print(functions.word_count_f(10,2))" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "333f0083", "metadata": {}, "outputs": [], "source": [ - "from functions import addition_f as addf, subtraction_f as subf" + "from functions import get_unique_list_f as addf, count_case_f as subf" ] }, { @@ -657,7 +657,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 11, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], @@ -676,7 +676,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 14, "id": "52147e70", "metadata": {}, "outputs": [], @@ -687,14 +687,14 @@ " raise ValueError(\"n must be a non-negative integer\")\n", " \n", " sequence = []\n", - " for i in range(n+1):\n", + " for (i) in range(1, n+1):\n", " sequence.append(fibonacci_number(i))\n", " return sequence" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "bb9f3bcf", "metadata": {}, "outputs": [ @@ -703,8 +703,8 @@ "output_type": "stream", "text": [ "55\n", - "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n", - "[0, 1, 1, 2, 3, 5]\n" + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n", + "[1, 1, 2, 3, 5]\n" ] } ],