diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..e3acd12 --- /dev/null +++ b/functions.py @@ -0,0 +1,127 @@ +import string + +def get_unique_list_f(lst): + """ + Takes a list as an argument and returns a new list with unique elements from the first list. + Parameters: + lst (list): The input list. + Returns: + list: A new list with unique elements from the input list. + """ + # your code goes here + + try: + if type(lst) != list: + raise TypeError("Argument is not a list.") + if len(lst) == 0: + raise ValueError("Argument is empty, please try again.") + seen = set() + finished_lst = [] + for elem in lst: + if elem not in seen: + seen.add(elem) + finished_lst.append(elem) + except TypeError as e: + print(f"Error: {e}") + return [] + except ValueError as e: + print(f"Error: {e}") + return [] + else: + return finished_lst + finally: + print( + """ +Program finished successfully. Read the results and report any issue. +This program has been designed to iterate through a list and return another +list with just the unique elements of the first list. + """ + ) + +def count_case_f(string): + """ + Returns the number of uppercase and lowercase letters in the given string. + + Parameters: + string (str): The string to count uppercase and lowercase letters in. + + Returns: + A tuple containing the count of uppercase and lowercase letters in the string. + """ + # your code goes here + try: + if len(string) == 0: + raise ValueError("String is empty") + string = string.strip().split() + string = "".join(string) + uppercase_letters = "" + lowercase_letters = "" + for char in string: + if char.islower(): + lowercase_letters = char + lowercase_letters + else: + uppercase_letters = char + uppercase_letters + solution = (len(uppercase_letters), len(lowercase_letters)) + except ValueError as e: + print(f"Error: {e}") + return (0, 0) + except TypeError as e: + print(f'Error: {e}') + return (0, 0) + except AttributeError as e: + print(f'Error: this program works with strings, {e}.') + return (0, 0) + else: + print(f'Uppercase count: {solution[0]}, Lowercase count: {solution[1]}'); return solution + finally: + print("This program run succesfully.") + +def remove_punctuation_f(sentence): + """ + Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + str: The sentence without any punctuation marks. + """ + # your code goes here + try: + if len(sentence) == 0: + raise ValueError("String is empty.") + solution = sentence.replace(",", "").replace(".", "").replace("!", "").replace("¡", "").replace("?", "").replace("¿", "").replace(":", "") + except AttributeError as e: + print(f"Error: {e}") + except ValueError as e: + print(f"Error: {e}") + else: + return solution + finally: + print("remove_puntuation function ran succesfully.") + +# your code goes here + +def word_count_f(sentence): + """ + Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence. + Note: A word is defined as a sequence of characters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + int: The number of words in the sentence. + """ + # your code goes here + try: + no_punctuation = remove_punctuation_f(sentence) + solution = len(no_punctuation.split()) + except AttributeError as e: + print(f"Error: {e}") + except TypeError as e: + print(f"Error: {e}") + else: + print(f"In sentence provided: '{sentence}', there are: {solution} words."); return solution + finally: + print("Word_count function ran succesfully.") \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..74f7298 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,22 +28,63 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", + "execution_count": 22, + "id": "dcdbc93b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Program finished successfully. Read the results and report any issue.\n", + "This program has been designed to iterate through a list and return another\n", + "list with just the unique elements of the first list.\n", + " \n", + "[1, 2, 3, 'mama']\n" + ] + } + ], "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", - "\n", " 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", + "\n", + " try:\n", + " if type(lst) != list:\n", + " raise TypeError(\"Argument is not a list.\")\n", + " if len(lst) == 0:\n", + " raise ValueError(\"Argument is empty, please try again.\")\n", + " seen = set()\n", + " finished_lst = []\n", + " for elem in lst:\n", + " if elem not in seen:\n", + " seen.add(elem)\n", + " finished_lst.append(elem)\n", + " except TypeError as e:\n", + " print(f\"Error: {e}\")\n", + " return []\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return []\n", + " else:\n", + " return finished_lst\n", + " finally:\n", + " print(\n", + " \"\"\"\n", + "Program finished successfully. Read the results and report any issue.\n", + "This program has been designed to iterate through a list and return another\n", + "list with just the unique elements of the first list.\n", + " \"\"\"\n", + " )\n", + "\n", + "print(get_unique_list([1, 2, 2, 3, 3, \"mama\", \"mama\"]))" ] }, { @@ -60,10 +101,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 0, Lowercase count: 15\n", + "This program run succesfully.\n", + "(0, 15)\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +126,35 @@ " 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", + " try:\n", + " if len(string) == 0:\n", + " raise ValueError(\"String is empty\")\n", + " string = string.strip().split()\n", + " string = \"\".join(string)\n", + " uppercase_letters = \"\"\n", + " lowercase_letters = \"\"\n", + " for char in string:\n", + " if char.islower():\n", + " lowercase_letters = char + lowercase_letters\n", + " else:\n", + " uppercase_letters = char + uppercase_letters\n", + " solution = (len(uppercase_letters), len(lowercase_letters))\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return (0, 0)\n", + " except TypeError as e:\n", + " print(f'Error: {e}')\n", + " return (0, 0)\n", + " except AttributeError as e:\n", + " print(f'Error: this program works with strings, {e}.')\n", + " return (0, 0)\n", + " else:\n", + " print(f'Uppercase count: {solution[0]}, Lowercase count: {solution[1]}'); return solution\n", + " finally:\n", + " print(\"This program run succesfully.\")\n", + "\n", + "print(count_case(\"ddasdasd sakskdk\"))" ] }, { @@ -92,10 +171,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "remove_puntuation function run succesfully.\n", + "Tengo Una cosa para ti\n" + ] + } + ], "source": [ "import string\n", "\n", @@ -110,6 +198,50 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " try:\n", + " if len(sentence) == 0:\n", + " raise ValueError(\"String is empty.\")\n", + " solution = sentence.replace(\",\", \"\").replace(\".\", \"\").replace(\"!\", \"\").replace(\"¡\", \"\").replace(\"?\", \"\").replace(\"¿\", \"\").replace(\":\", \"\")\n", + " except AttributeError as e:\n", + " print(f\"Error: {e}\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " else:\n", + " return solution\n", + " finally: \n", + " print(\"remove_puntuation function run succesfully.\")\n", + "\n", + "print(remove_punctuation(\"Tengo! ¿Una cosa para ti?\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "remove_puntuation function run succesfully.\n", + "In sentence provided: 'This is a sentente to count.', there are: 6 words.\n", + "Word_count function run succesfully.\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code goes here\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +254,19 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " try:\n", + " no_punctuation = remove_punctuation(sentence)\n", + " solution = len(no_punctuation.split())\n", + " except AttributeError as e:\n", + " print(f\"Error: {e}\")\n", + " except TypeError as e:\n", + " print(f\"Error: {e}\")\n", + " else:\n", + " print(f\"In sentence provided: '{sentence}', there are: {solution} words.\"); return solution\n", + " finally:\n", + " print(\"Word_count function run succesfully.\")\n", + "word_count(\"This is a sentente to count.\")" ] }, { @@ -138,16 +282,6 @@ "*would give you as expected output: 7*" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, { "cell_type": "markdown", "id": "fb01ccba-2ecc-4eea-b9a9-2bf9d29e8804", @@ -171,9 +305,63 @@ "execution_count": null, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-15\n", + "This program run succesfully.\n" + ] + }, + { + "data": { + "text/plain": [ + "-15" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def addition(x, y):\n", + " return x + y\n", + "def multiplication(x, y):\n", + " return x * y\n", + "def substraction(x, y):\n", + " return x - y\n", + "def division(x, y):\n", + " return x / y\n", + "def calculate(x, y, operator):\n", + " try:\n", + " if operator == \"+\":\n", + " calculation = addition(x, y)\n", + " elif operator == \"-\":\n", + " calculation = substraction(x, y)\n", + " elif operator == \"/\":\n", + " try:\n", + " if y == 0:\n", + " raise ZeroDivisionError(\"ZeroDivisionError: You cannot divide by 0 in this program.\")\n", + " except ZeroDivisionError as e:\n", + " print(f\"Error: {e}\")\n", + " else:\n", + " calculation = division(x, y)\n", + " elif operator == \"*\":\n", + " calculation = multiplication(x, y)\n", + " else:\n", + " raise ValueError(\"The operator argument should be '+', '*', '/', '-'.\")\n", + " except ValueError as e:\n", + " print(f'Error: {e}')\n", + " else:\n", + " print(calculation); return calculation\n", + " finally:\n", + " print('This program run succesfully.')\n", + "\n", + "calculate(5, 20, \"-\")\n" ] }, { @@ -193,11 +381,85 @@ { "cell_type": "code", "execution_count": null, - "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", + "id": "7d854328", "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-2011\n", + "This program was executed.\n" + ] + }, + { + "data": { + "text/plain": [ + "-2011" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def addition(*args):\n", + " return sum(args)\n", + "def multiplication(*args):\n", + " multiplication = args[0]\n", + " for arg in args[1:]:\n", + " multiplication *= arg\n", + " return multiplication\n", + "def substraction(*args):\n", + " substraction = args[0]\n", + " for arg in args[1:]:\n", + " substraction -= arg\n", + " return substraction\n", + "def division(*args):\n", + " division = args[0]\n", + " for arg in args[1:]:\n", + " division /= arg\n", + " return division\n", + "\n", + "def calculate(*args):\n", + " try:\n", + " if \"+\" in args:\n", + " lst_nums = tuple(args[:-1])\n", + " calculation = addition(*lst_nums)\n", + " elif \"-\" in args:\n", + " lst_nums = tuple(args[:-1])\n", + " calculation = substraction(*lst_nums)\n", + " elif \"/\" in args:\n", + " if 0 in args[0:-1]:\n", + " raise ZeroDivisionError(\"You cannot enter a zero in this program.\")\n", + " lst_nums = tuple(args[:-1])\n", + " calculation = division(*lst_nums)\n", + " elif args[-1] == \"*\":\n", + " lst_nums = tuple(args[:-1])\n", + " calculation = multiplication(*lst_nums)\n", + " else:\n", + " raise ValueError(\"The operator argument should be '+', '*', '/', '-', and it should be at the end of the tuple.\")\n", + " except ValueError as e:\n", + " print(f'Error: {e}')\n", + " except ZeroDivisionError as e:\n", + " print(f'Error: {e}')\n", + " else:\n", + " print(calculation); return calculation\n", + " finally:\n", + " print('This program was executed.')\n", + "\n", + "calculate(-1, 0, 2000, +10, \"-\")" ] }, { @@ -273,16 +535,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "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", + "\n", + "Program finished successfully. Read the results and report any issue.\n", + "This program has been designed to iterate through a list and return another\n", + "list with just the unique elements of the first list.\n", + " \n", + "[1, 2, 3, 'mama']\n", + "Uppercase count: 0, Lowercase count: 15\n", + "This program run succesfully.\n", + "(0, 15)\n", + "remove_puntuation function ran succesfully.\n", + "Tengo Una cosa para ti\n", + "remove_puntuation function ran succesfully.\n", + "In sentence provided: 'This is a sentente to count.', there are: 6 words.\n", + "Word_count function ran succesfully.\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 41, + "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" + "%autoreload 2\n", + "\n", + "# your code goes here\n", + "\n", + "from functions import get_unique_list_f\n", + "from functions import count_case_f\n", + "from functions import remove_punctuation_f\n", + "from functions import word_count_f\n", + "print(get_unique_list_f([1, 2, 2, 3, 3, \"mama\", \"mama\"]))\n", + "print(count_case_f(\"ddasdasd sakskdk\"))\n", + "print(remove_punctuation_f(\"Tengo! ¿Una cosa para ti?\"))\n", + "word_count_f(\"This is a sentente to count.\")\n" ] }, { @@ -315,18 +619,106 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "# F(0) = 0\n", + "# F(1) = 1\n", + "# F(n) = f(n-1) + f(n-2)\n", + "\n", + "def fibo(n):\n", + " if n == 0:\n", + " fibo_sequence = [0]\n", + " elif n == 1:\n", + " fibo_sequence = [0, 1]\n", + " elif n >= 2:\n", + " fibo_sequence = [0, 1]\n", + " for i in range(2, n + 1):\n", + " next_f_n = fibo_sequence[-1] + fibo_sequence[-2]\n", + " fibo_sequence.append(next_f_n)\n", + " return fibo_sequence\n", + "\n", + "print(fibo(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "b1002f96", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,\n", + " 1,\n", + " 1,\n", + " 2,\n", + " 3,\n", + " 5,\n", + " 8,\n", + " 13,\n", + " 21,\n", + " 34,\n", + " 55,\n", + " 89,\n", + " 144,\n", + " 233,\n", + " 377,\n", + " 610,\n", + " 987,\n", + " 1597,\n", + " 2584,\n", + " 4181,\n", + " 6765]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# F(0) = 0\n", + "# F(1) = 1\n", + "# F(n) = f(n-1) + f(n-2)\n", + "\n", + "def fibo(n):\n", + " if n == 0:\n", + " fibo_n = 0\n", + " elif n == 1:\n", + " fibo_n = 1\n", + " elif n >= 2:\n", + " fibo_n = fibo(n-1) + fibo(n-2)\n", + " return fibo_n\n", + "\n", + "def fibo_sequence(n):\n", + " fibo_sequence = []\n", + " for number in range(0, n + 1):\n", + " fibo_n = fibo(number)\n", + " fibo_sequence.append(fibo_n)\n", + " return fibo_sequence\n", + "\n", + "fibo(20)\n", + "fibo_sequence(20)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +732,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,