diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..46b0b39 --- /dev/null +++ b/functions.py @@ -0,0 +1,16 @@ +# functions.py + +def get_unique_list_f(lst): + return list(set(lst)) + +def count_case_f(text): + mayus = sum(1 for c in text if c.isupper()) + minus = sum(1 for c in text if c.islower()) + return {"Uppercase": mayus, "Lowercase": minus} + +def remove_punctuation_f(text): + import string + return text.translate(str.maketrans('', '', string.punctuation)) + +def word_count_f(text): + return len(text.split()) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..985ab67 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +51,12 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " new_set = set(lst)\n", + " new_lst = list(new_set)\n", + " return(new_lst)\n", + "\n", + "Input = [1,2,3,3,3,3,4,5] \n", + "print(get_unique_list(Input))" ] }, { @@ -63,7 +76,16 @@ "execution_count": null, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2, Lowercase count: 7\n", + "(2, 7)\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +97,26 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " \n", + " # upper_count = 0\n", + " # lower_count = 0\n", + "\n", + " # for char in string:\n", + " # if char.isupper():\n", + " # upper_count += 1\n", + " # elif char.islower():\n", + " # lower_count += 1\n", + " #print(f\"Uppercase count: {upper}, Lowercase count: {lower}\")\n", + " # return (upper_count, lower_count)\n", + "\n", + "## ALTERNATIVE ##\n", + "\n", + " upper = sum(1 for c in string if c.isupper())\n", + " lower = sum(1 for c in string if c.islower())\n", + " print(f\"Uppercase count: {upper}, Lowercase count: {lower}\")\n", + " return (upper, lower)\n", + "\n", + "print(count_case(\"Hallo Word!\")) " ] }, { @@ -92,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -109,7 +150,14 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " # punctuation = \",:;-_#.¿?¡!()\"\n", + " # for char in punctuation:\n", + " # sentence = sentence.replace(char, \"\")\n", + " \n", + " # return sentence\n", + "\n", + " clean_sentence = \"\".join(c for c in sentence if c.isalnum() or c.isspace())\n", + " return clean_sentence\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +170,9 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " new_sentence = remove_punctuation(sentence)\n", + " words = new_sentence.split()\n", + " return len(words)" ] }, { @@ -140,12 +190,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "sentence = \"Note: this is an example !!! Good day ; )\"\n", + "print(remove_punctuation(sentence))\n", + "print(word_count(sentence))" ] }, { @@ -168,12 +229,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " --- Python Calculator ---\n", + "-------------------------------\n", + "Result: 24.0 + 26.0 = 50.0\n", + "-------------------------------\n" + ] + } + ], "source": [ - "# your code goes here" + "# --- Arithmetic Functions ---\n", + "def add(a, b): return a + b\n", + "def subtract(a, b): return a - b\n", + "def multiply(a, b): return a * b\n", + "def divide(a, b):\n", + " if b == 0:\n", + " return \"Error: you cant divide between zero\"\n", + " return a / b\n", + "\n", + "# --- Controller Function ---\n", + "def calculate(operand1, operand2, operator):\n", + " if operator == \"+\":\n", + " return add(operand1, operand2)\n", + " elif operator == \"-\":\n", + " return subtract(operand1, operand2)\n", + " elif operator == \"*\":\n", + " return multiply(operand1, operand2)\n", + " elif operator == \"/\":\n", + " return divide(operand1, operand2)\n", + " else:\n", + " return \"Not valid operator\"\n", + " \n", + "\n", + "# --- Main Program (User Interface) ---\n", + "print(\" --- Python Calculator ---\")\n", + "\n", + "try:\n", + " # Taking inputs from the user\n", + " num1 = float(input(\"Enter the first number: \"))\n", + " op = input(\"Enter the operator (+, -, *, /): \")\n", + " num2 = float(input(\"Enter the second number: \"))\n", + "\n", + " # Processing the calculation\n", + " result = calculate(num1, num2, op)\n", + "\n", + " # Displaying the result\n", + " print(\"-\" * 31)\n", + " print(f\"Result: {num1} {op} {num2} = {result}\")\n", + " print(\"-\" * 31)\n", + "\n", + "except ValueError:\n", + " print(\"Invalid input! Please enter numeric values for the numbers.\")" ] }, { @@ -192,12 +305,87 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Advanced Multi-Number Calculator ---\n", + "------------------------------\n", + "Numbers: [155.0, 5.0, 14.0, 26.0, 33.0, 17.0]\n", + "Operation: +\n", + "Final Result: 250.0\n", + "------------------------------\n" + ] + } + ], "source": [ - "# your code goes here" + "# --- Arithmetic Functions ---\n", + "def add(*args):return sum(args)\n", + "\n", + "def subtract(*args):\n", + " # Starts with the first number and subtracts the rest\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(a, b):\n", + " return a / b if b != 0 else \"Error: Division by zero\"\n", + "\n", + "# --- Controller Function ---\n", + "def calculate(operator, *args):\n", + " if not args:\n", + " return \"Error: No numbers provided\"\n", + "\n", + " if operator == \"+\":\n", + " return add(*args)\n", + " elif operator == \"-\":\n", + " return subtract(*args)\n", + " elif operator == \"*\":\n", + " return multiply(*args)\n", + " elif operator == \"/\":\n", + " if len(args) != 2:\n", + " return \"Error: Division requires exactly 2 numbers\"\n", + " return divide(args[0], args[1])\n", + " else:\n", + " return \"Error: Invalid operator\"\n", + " \n", + "\n", + "\n", + "# --- Main Program (User Interface) ---\n", + "print(\"--- Advanced Multi-Number Calculator ---\")\n", + "\n", + "try:\n", + " op = input(\"Enter the operator (+, -, *, /): \")\n", + " # Input example: \"10 5 2\"\n", + " raw_numbers = input(\"Enter numbers separated by spaces: \")\n", + " \n", + " # Convert the string of numbers into a list of floats\n", + " num_list = [float(n) for n in raw_numbers.split()]\n", + "\n", + " # We pass the operator and 'unpack' the list into *args using the *\n", + " result = calculate(op, *num_list)\n", + "\n", + " print(\"-\" * 30)\n", + " print(f\"Numbers: {num_list}\")\n", + " print(f\"Operation: {op}\")\n", + " print(f\"Final Result: {result}\")\n", + " print(\"-\" * 30)\n", + "\n", + "except ValueError:\n", + " print(\"Invalid input! Please enter numbers separated by spaces.\")\n", + "except IndexError:\n", + " print(\"Error: You must enter at least one number.\")" ] }, { @@ -273,16 +461,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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", + "Uniques: [1, 2, 3, 4, 5]\n", + "Case Counter: {'Uppercase': 4, 'Lowercase': 22}\n", + "Without Punctuation: Hallo Word Python is Amaziiing\n", + "Total Words: 5\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f\n", + "\n", + "# Test\n", + "test_lista = [1, 2, 2, 3, 4, 4, 5]\n", + "test_texto = \"Hallo Word, Python is Amaziiing!\"\n", + "\n", + "print(f\"Uniques: {get_unique_list_f(test_lista)}\")\n", + "print(f\"Case Counter: {count_case_f(test_texto)}\")\n", + "print(f\"Without Punctuation: {remove_punctuation_f(test_texto)}\")\n", + "print(f\"Total Words: {word_count_f(test_texto)}\")" ] }, { @@ -315,18 +525,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fibonacci sequence for n = 13:\n", + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]\n" + ] + } + ], "source": [ - "# your code goes here" + "from functools import lru_cache\n", + "\n", + "@lru_cache(maxsize=None)\n", + "def fibonacci_recursive(n):\n", + " if n <= 0: return 0\n", + " if n == 1: return 1\n", + " return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)\n", + "\n", + "def generate_fib_sequence(n):\n", + " sequence = []\n", + " for i in range(1, n + 1):\n", + " sequence.append(fibonacci_recursive(i))\n", + " return sequence\n", + "\n", + "# Testing the function\n", + "n_terms = 13\n", + "result = generate_fib_sequence(n_terms)\n", + "\n", + "print(f\"Fibonacci sequence for n = {n_terms}:\")\n", + "print(result)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +578,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,