diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000..a2cf9ba Binary files /dev/null and b/__pycache__/functions.cpython-313.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..53ddda7 --- /dev/null +++ b/functions.py @@ -0,0 +1,93 @@ +def get_unique_list(lst): + return list(set(lst)) + +def count_case(string): + mayusculas = 0 + minusculas = 0 + + for char in string: + if char.isupper(): + mayusculas += 1 + elif char.islower(): + minusculas += 1 + + return (f"Mayusculas son: {mayusculas}", f"Minusculas son: {minusculas}") + +import string + +def remove_punctuation(sentence): + translator = str.maketrans('', '', string.punctuation) + + return sentence.translate(translator) + +def word_count(sentence): + punctuation_to_remove = string.punctuation + "¿¡" + translator = str.maketrans('', '', punctuation_to_remove) + clean_sentence = sentence.translate(translator) + words = clean_sentence.split() + + return len(words) + + +def sumar(a, b): + return a + b + +def restar(a, b): + return a - b + +def multiplicar(a, b): + return a * b + +def dividir(a, b): + if b == 0: + return "Error: No se puede dividir por cero" + return a / b + + +def calculate(n1, n2, operador): + if operador == '+': + return sumar(n1, n2) + elif operador == '-': + return restar(n1, n2) + elif operador == '*': + return multiplicar(n1, n2) + elif operador == '/': + return dividir(n1, n2) + else: + return "Operador no válido" + + + +def restar(*args): + resultado = args[0] + for num in args[1:]: + resultado -= num + return resultado + +def multiplicar(*args): + resultado = 1 + for num in args: + resultado *= num + return resultado + +def dividir(a, b): + if b == 0: + return "Error: No se puede dividir por cero" + return a / b + +def calculate(operador, *args): + if not args: + return "Error: No se proporcionaron números" + + if operador == '+': + return sumar(*args) + elif operador == '-': + return restar(*args) + elif operador == '*': + return multiplicar(*args) + elif operador == '/': + if len(args) != 2: + return "Error: La división requiere exactamente dos números" + return dividir(args[0], args[1]) + else: + return "Operador no válido" \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..11343e3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -43,7 +43,27 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " return list(set(lst))\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cca05bb4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "p=get_unique_list([1, 2, 2, 3, 4, 4, 5])\n", + "print(p)" ] }, { @@ -60,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -73,9 +93,37 @@ " 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", + " A tuple containing the count of uppercase and lowercase letters in the string. \n", " \"\"\"\n", - " # your code goes here" + " mayusculas = 0\n", + " minusculas = 0\n", + " \n", + " for char in string:\n", + " if char.isupper():\n", + " mayusculas += 1\n", + " elif char.islower():\n", + " minusculas += 1\n", + " \n", + " return (f\"Mayusculas son: {mayusculas}\", f\"Minusculas son: {minusculas}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4796bc93", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Mayusculas son: 2', 'Minusculas son: 8')\n" + ] + } + ], + "source": [ + "s=count_case(\"Hello World\")\n", + "print(s)" ] }, { @@ -92,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -109,7 +157,9 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " translator = str.maketrans('', '', string.punctuation)\n", + " \n", + " return sentence.translate(translator)\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,12 +172,38 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " punctuation_to_remove = string.punctuation + \"¿¡\"\n", + " translator = str.maketrans('', '', punctuation_to_remove)\n", + " clean_sentence = sentence.translate(translator)\n", + " words = clean_sentence.split()\n", + " \n", + " return len(words)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "401673cc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Note this is an example Good day '" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "remove_punctuation(\"Note : this is an example !!! Good day : )\")\n" ] }, { "cell_type": "markdown", - "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", + "id": "69a46687", "metadata": {}, "source": [ "*For example, calling*\n", @@ -140,12 +216,23 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", + "execution_count": 20, + "id": "594b7fdc", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "word_count(\"Note : this is an example !!! Good day : )\")" ] }, { @@ -168,12 +255,127 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def sumar(a, b):\n", + " return a + b\n", + "\n", + "def restar(a, b):\n", + " return a - b\n", + "\n", + "def multiplicar(a, b):\n", + " return a * b\n", + "\n", + "def dividir(a, b):\n", + " if b == 0:\n", + " return \"Error: No se puede dividir por cero\"\n", + " return a / b\n", + "\n", + "\n", + "def calculate(n1, n2, operador):\n", + " if operador == '+':\n", + " return sumar(n1, n2)\n", + " elif operador == '-':\n", + " return restar(n1, n2)\n", + " elif operador == '*':\n", + " return multiplicar(n1, n2)\n", + " elif operador == '/':\n", + " return dividir(n1, n2)\n", + " else:\n", + " return \"Operador no válido\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1077f1e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Suma (10 + 5): 15\n" + ] + } + ], + "source": [ + "print(f\"Suma (10 + 5): {calculate(10, 5, '+')}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bddd8fe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Resta (10 - 5): 5\n" + ] + } + ], + "source": [ + "print(f\"Resta (10 - 5): {calculate(10, 5, '-')}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "8b6e462c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Multiplicación (10 * 5): 50\n" + ] + } + ], + "source": [ + "print(f\"Multiplicación (10 * 5): {calculate(10, 5, '*')}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "a44b7c7f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "División por cero: Error: No se puede dividir por cero\n" + ] + } + ], + "source": [ + "print(f\"División por cero: {calculate(10, 0, '/')}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "6e1144a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "División (10 / 5): 2.0\n" + ] + } + ], + "source": [ + "print(f\"División (10 / 5): {calculate(10, 5, '/')}\")\n" ] }, { @@ -192,12 +394,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def sumar(*args):\n", + " return sum(args)\n", + "\n", + "def restar(*args):\n", + " resultado = args[0]\n", + " for num in args[1:]:\n", + " resultado -= num\n", + " return resultado\n", + "\n", + "def multiplicar(*args):\n", + " resultado = 1\n", + " for num in args:\n", + " resultado *= num\n", + " return resultado\n", + "\n", + "def dividir(a, b):\n", + " if b == 0:\n", + " return \"Error: No se puede dividir por cero\"\n", + " return a / b\n", + "\n", + "def calculate(operador, *args):\n", + " if not args:\n", + " return \"Error: No se proporcionaron números\"\n", + "\n", + " if operador == '+':\n", + " return sumar(*args)\n", + " elif operador == '-':\n", + " return restar(*args)\n", + " elif operador == '*':\n", + " return multiplicar(*args)\n", + " elif operador == '/':\n", + " if len(args) != 2:\n", + " return \"Error: La división requiere exactamente dos números\"\n", + " return dividir(args[0], args[1])\n", + " else:\n", + " return \"Operador no válido\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4a10e17c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Suma múltiple (10 + 5 + 2 + 3): 20\n" + ] + } + ], + "source": [ + "print(f\"Suma múltiple (10 + 5 + 2 + 3): {calculate('+', 10, 5, 2, 3)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "be59f899", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Resta múltiple (100 - 20 - 10): 70\n" + ] + } + ], + "source": [ + "print(f\"Resta múltiple (100 - 20 - 10): {calculate('-', 100, 20, 10)}\")\n" ] }, { @@ -273,16 +546,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" + ] + }, + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "import functions as f\n", + "\n", + "f.get_unique_list([1, 2, 2, 3, 4, 4, 5])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "041c2048", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('Mayusculas son: 2', 'Minusculas son: 8')" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f.count_case(\"Hello World!\")" ] }, { @@ -313,20 +628,115 @@ "*Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]*" ] }, + { + "cell_type": "markdown", + "id": "71cdd652", + "metadata": {}, + "source": [ + "hace que me entregue el numero que ocupa la posición" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def fibonacci_recursivo(n):\n", + " \n", + " if n == 0:\n", + " return 0\n", + " if n == 1:\n", + " return 1\n", + " \n", + " return fibonacci_recursivo(n - 1) + fibonacci_recursivo(n - 2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "37697fbf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fibonacci_recursivo(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "d5302d68", + "metadata": {}, + "outputs": [], + "source": [ + "def generar_lista_fibonacci(n):\n", + " lista = [0]\n", + " \n", + " for i in range(1, n + 1):\n", + " valor = fibonacci_recursivo(i)\n", + " lista.append(valor)\n", + " \n", + " return lista\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "cd7baf52", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generar_lista_fibonacci(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "1bb32d31", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generar_lista_fibonacci(13)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +750,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.1" } }, "nbformat": 4,