From b3a7c1aba5a4bc00e60d36cba1a0f2cf00fc4cdc Mon Sep 17 00:00:00 2001 From: Lautaro Date: Thu, 5 Feb 2026 14:08:14 +0100 Subject: [PATCH] lab done --- __pycache__/functions.cpython-313.pyc | Bin 0 -> 1302 bytes functions.py | 28 +++ lab-python-functions.ipynb | 297 +++++++++++++++++++++++--- 3 files changed, 292 insertions(+), 33 deletions(-) create mode 100644 __pycache__/functions.cpython-313.pyc create mode 100644 functions.py diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3a8a055e5a91536016dc8a25dcf6fde717325855 GIT binary patch literal 1302 zcmaJ=&uY#4#wcXdDW?+^FGB1vPKxe zp_O15a=!yGy`(SnI8fEGO?CanuDAOEN~EYwq_KwNf5Mf%&||Edgzj)%6>ly73ubHG zS}p$vfC*!8``Dy54whcv>SDK556E!8E5dr6ay9Y%70N9`rWA*4sp7JF7|2AtpNMv) z;oP^mP<)J`@gz=U%JD-MR2*ASa6Z8B4CmT?L|6;Yg zt6^ABIy_`WyqR*}Zg4?0eaNP|jPL2v(h3r+gd9NWFyYw&MR^8X4%F`O%o&tMxTaEJ>CNTE6W(H#9?j=pqW)n3%z zTzRLxpZhpD_hE8w>+JlNncFmS-xJgmVgWz0|KaUDH%3b2|3t;Ka*wu>wnqY2_iyP@ zpE4opo{NvvqYOi*aYyz$9<>>*%EXG#t3s$&_SB+cl~^*1{5)=*!XeHf;OiPZ|N7Uh z!OM;Lc51klZ|2wkY|MT(2iL;&-!_dC(J8G2R~6C2jMNtO5q;?`L>V!+$?2M34Lv%; m$1z>$U5p}p)d?Y=AidK^$jDCe2>Eemgpli`lhnx%QTaE|y9g`* literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..52f877f --- /dev/null +++ b/functions.py @@ -0,0 +1,28 @@ +import string + + +def get_unique_list_f(lst): + return list(set(lst)) + + +def count_case_f(string_input): + upper = 0 + lower = 0 + + for char in string_input: + if char.isupper(): + upper += 1 + elif char.islower(): + lower += 1 + + return upper, lower + + +def remove_punctuation_f(sentence): + return sentence.translate(str.maketrans('', '', string.punctuation)) + + +def word_count_f(sentence): + cleaned = remove_punctuation_f(sentence) + words = cleaned.split() + return len(words) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..f09ae86 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,22 +28,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], "source": [ - "def get_unique_list(lst):\n", - " \"\"\"\n", - " Takes a list as an argument and returns a new list with unique elements from the first list.\n", "\n", - " Parameters:\n", - " lst (list): The input list.\n", + "def get_unique_list(lst):\n", "\n", - " Returns:\n", - " list: A new list with unique elements from the input list.\n", - " \"\"\"\n", - " # your code goes here\n" + " unique = []\n", + " \n", + " for item in lst:\n", + " if item not in unique:\n", + " unique.append(item)\n", + " \n", + " return unique\n", + "\n" ] }, { @@ -60,7 +60,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -68,14 +68,17 @@ "def count_case(string):\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", - "\n", - " Parameters:\n", - " string (str): The string to count uppercase and lowercase letters in.\n", - "\n", - " Returns:\n", - " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " upper = 0\n", + " lower = 0\n", + " \n", + " for char in string:\n", + " if char.isupper():\n", + " upper += 1\n", + " elif char.islower():\n", + " lower += 1\n", + " \n", + " return (upper, lower)\n" ] }, { @@ -92,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -109,7 +112,16 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + "\n", + "\n", + " cleaned = \"\"\n", + " \n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " cleaned += char\n", + " \n", + " return cleaned\n", + "\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +134,13 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goedef word_count(sentence):\n", + " \"\"\"\n", + " Counts the number of words in a given sentence.\n", + " \"\"\"\n", + " cleaned = remove_punctuation(sentence)\n", + " words = cleaned.split()\n", + " return len(words)\n" ] }, { @@ -140,12 +158,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "word_count ('Amazin job')" ] }, { @@ -168,12 +197,73 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def add(x, y):\n", + " return x + y\n", + "\n", + "def subtract(x, y):\n", + " return x - y\n", + "\n", + "def multiply(x, y):\n", + " return x * y\n", + "\n", + "def divide(x, y):\n", + " if y == 0:\n", + " return \"Error: Cannot divide by zero\"\n", + " return x / y\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "8dd7a19b", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate(x, y, operator):\n", + " \n", + " if operator == \"+\":\n", + " return add(x, y)\n", + " \n", + " elif operator == \"-\":\n", + " return subtract(x, y)\n", + " \n", + " elif operator == \"*\":\n", + " return multiply(x, y)\n", + " \n", + " elif operator == \"/\":\n", + " return divide(x, y)\n", + " \n", + " else:\n", + " return \"Operador no reconocido\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "820a234e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "10\n", + "200\n", + "2.0\n" + ] + } + ], + "source": [ + "print(calculate(20, 10, \"+\"))\n", + "print(calculate(20, 10, \"-\"))\n", + "print(calculate(20, 10, \"*\"))\n", + "print(calculate(20, 10, \"/\"))\n" ] }, { @@ -192,12 +282,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def add(*args):\n", + " return sum(args)\n", + "\n", + "def subtract(*args):\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "def multiply(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "def divide(*args):\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " if num == 0:\n", + " return \"Error: Cannot divide by zero\"\n", + " result /= num\n", + " return result\n", + "\n", + "\n", + "def calculate(operator, *args):\n", + " \n", + " if operator == \"+\":\n", + " return add(*args)\n", + " \n", + " elif operator == \"-\":\n", + " return subtract(*args)\n", + " \n", + " elif operator == \"*\":\n", + " return multiply(*args)\n", + " \n", + " elif operator == \"/\":\n", + " return divide(*args)\n", + " \n", + " else:\n", + " return \"Invalid operator\"" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "68f8b84b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "64\n", + "30\n", + "10.0\n" + ] + } + ], + "source": [ + "print(calculate(\"+\", 2, 4, 8, 16)) \n", + "print(calculate(\"*\", 2, 4, 8)) \n", + "print(calculate(\"-\", 100, 50, 20)) \n", + "print(calculate(\"/\", 100, 2, 5)) " ] }, { @@ -273,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, "outputs": [], @@ -285,6 +438,44 @@ "# your code goes here" ] }, + { + "cell_type": "code", + "execution_count": 18, + "id": "0416fffb", + "metadata": {}, + "outputs": [], + "source": [ + " \n", + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "15c1d2ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n", + "(1, 13)\n", + "Hey darling Its such a pleasure\n", + "2\n" + ] + } + ], + "source": [ + "print(get_unique_list_f([1,2,2,3,4,4]))\n", + "\n", + "print(count_case_f(\"Amazing job, team\"))\n", + "\n", + "print(remove_punctuation_f(\"Hey, darling! It's such a pleasure\"))\n", + "\n", + "print(word_count_f(\"You're incredible\"))\n" + ] + }, { "cell_type": "markdown", "id": "06d0eb13-8dcb-4783-8f74-8f71beb2d0b4", @@ -315,18 +506,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def fibonacci(n):\n", + " \n", + " if n == 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + "\n", + " else:\n", + " return fibonacci(n - 1) + fibonacci(n - 2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "4be8b4c8", + "metadata": {}, + "outputs": [], + "source": [ + "def fibonacci_sequence(n):\n", + " sequence = []\n", + " for i in range(1, n + 1):\n", + " sequence.append(fibonacci(i))\n", + " return sequence\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "3ba2852b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n" + ] + } + ], + "source": [ + "print(fibonacci_sequence(10))\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +571,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,