diff --git a/__pycache__/functions.cpython-310.pyc b/__pycache__/functions.cpython-310.pyc new file mode 100644 index 0000000..7993877 Binary files /dev/null and b/__pycache__/functions.cpython-310.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..4280d00 --- /dev/null +++ b/functions.py @@ -0,0 +1,74 @@ +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 + elements_unique = list(set(lst)) + return elements_unique + +#Subtraction function +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 + upper_case = 0 + lower_case = 0 + for letra in string: + if letra.isupper(): + upper_case += 1 + elif letra == ' ': + pass + else: + lower_case += 1 + return (upper_case, lower_case) + +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 + result = "" + for char in sentence: + if char not in string.punctuation: + result+= char + + return result + +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 + + clean = remove_punctuation_f(sentence) + words = clean.split() + return len(words) + diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..623c10d 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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": 7, + "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", + " return (upper_case, lower_case)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "9749a83d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 8)\n" + ] + } + ], + "source": [ + "input_string = \"Hello World\"\n", + "funcion_count_case = count_case(input_string)\n", + "print(funcion_count_case)" ] }, { @@ -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)" ] }, { @@ -273,7 +527,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, + "id": "0b2ea4e1", + "metadata": {}, + "outputs": [], + "source": [ + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "e5300864", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 3, 5, 6, 7, 8]\n" + ] + } + ], + "source": [ + "print(get_unique_list_f([3,5,7,5,8,6,0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "8cf3a9a4", + "metadata": {}, + "outputs": [], + "source": [ + "import functions" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "3f48f134", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 7)\n", + "Note this is an example Good day \n" + ] + } + ], + "source": [ + "print(functions.count_case_f(\"Como eStas\"))\n", + "print(functions.remove_punctuation_f(\"Note : this is an example !!! Good day : )\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "333f0083", + "metadata": {}, + "outputs": [], + "source": [ + "from functions import get_unique_list_f as unique_f, count_case_f as count_f" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "9b2004df", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 3, 5, 6, 7, 8]\n", + "(2, 7)\n" + ] + } + ], + "source": [ + "print(unique_f([3,5,7,5,8,6,0]))\n", + "print(count_f(\"Como eStas\"))" + ] + }, + { + "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": 11, "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": 14, + "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(1, n+1):\n", + " sequence.append(fibonacci_number(i))\n", + " return sequence" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "bb9f3bcf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n", + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n", + "[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,