diff --git a/__pycache__/functions.cpython-311.pyc b/__pycache__/functions.cpython-311.pyc new file mode 100644 index 0000000..6616237 Binary files /dev/null and b/__pycache__/functions.cpython-311.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..8b66fc3 --- /dev/null +++ b/functions.py @@ -0,0 +1,100 @@ +import string +import re + + +def addition_f(lst): + return sum(lst) + +def subtraction_f(lst): + if len(lst) == 0: + return + return lst[0] - sum(lst[1:]) + +def multiplication_f(lst): + result = 1 + for x in lst: + result *= x + return result + +def division_f(lst): + if len(lst) == 0: + return + result = lst[0] + for x in lst[1:]: + result /= x + return result + +def calculate_f(**kwargs): + """ + input = calculate_opt("-"=[1,2,3,4,5]) + returns 1-2-3-4-5 + + input = calculate_opt("+"=[1,2,3,4,5]) + returns 1# + + """ + results = [] + for operator, value in kwargs.items(): + if operator == "plus": + results.append(addition_f(value)) + elif operator == "minus": + results.append(subtraction_f(value)) + elif operator == "mult": + results.append(multiplication_f(value)) + elif operator == "div": + results.append(division_f(value)) + else: + print("Wrong operator!") + return results + +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. + """ + return list(set(lst)) + + +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. + """ + return (sum(x.isupper() for x in string), sum(x.islower() for x in string)) + + +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. + """ + pattern = r"\w+|[^\s\w^[']]+" + return " ".join(re.findall(pattern, sentence)) + +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. + """ + return len(remove_punctuation_f(sentence).split(" ")) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..a80296c 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +54,11 @@ " 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", + "\n", + "a = [1,2,3,3,3,3,4,5]\n", + "get_unique_list(a)" ] }, { @@ -60,10 +75,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 8)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +101,10 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " return (sum(x.isupper() for x in string), sum(x.islower() for x in string))\n", + "\n", + "string = \"Hello World\"\n", + "count_case(string)" ] }, { @@ -92,12 +121,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import string\n", + "import re\n", "\n", "def remove_punctuation(sentence):\n", " \"\"\"\n", @@ -109,7 +150,8 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " pattern = r\"\\w+|[^\\s\\w^[']]+\"\n", + " return \" \".join(re.findall(pattern, sentence))\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +164,10 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " return len(remove_punctuation(sentence).split(\" \"))\n", + "\n", + "a = \"Note : this is an example !!! Good day : )\"\n", + "word_count(a)" ] }, { @@ -138,16 +183,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", @@ -168,12 +203,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "2.5\n" + ] + } + ], "source": [ - "# your code goes here" + "def addition(a, b):\n", + " return a + b\n", + "\n", + "def subtraction(a, b):\n", + " return a - b\n", + "\n", + "def multiplication(a, b):\n", + " return a * b\n", + "\n", + "def division(a, b):\n", + " return a / b\n", + "\n", + "def calculate(a, b, operator):\n", + " if operator == \"+\":\n", + " return addition(a, b)\n", + " elif operator == \"-\":\n", + " return subtraction(a, b)\n", + " elif operator == \"*\": \n", + " return multiplication(a, b)\n", + " elif operator == \"/\":\n", + " return division(a, b)\n", + " else: \n", + " print(\"Wrong operator!\")\n", + "\n", + "print(calculate(2,2,\"+\"))\n", + "print(calculate(5,2,\"/\"))" ] }, { @@ -192,12 +261,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10]\n", + "[4]\n", + "[10, 4]\n", + "[10]\n", + "[2.5]\n", + "[10, 2.5]\n", + "[10, 4, 10, 2.5]\n", + "[0, None, 1, None]\n" + ] + } + ], "source": [ - "# your code goes here" + "def addition(lst):\n", + " return sum(lst)\n", + "\n", + "def subtraction(lst):\n", + " if len(lst) == 0: \n", + " return\n", + " return lst[0] - sum(lst[1:])\n", + "\n", + "def multiplication(lst):\n", + " result = 1\n", + " for x in lst:\n", + " result *= x\n", + " return result\n", + "\n", + "def division(lst):\n", + " if len(lst) == 0: \n", + " return\n", + " result = lst[0]\n", + " for x in lst[1:]:\n", + " result /= x\n", + " return result\n", + "\n", + "def calculate_opt(**kwargs):\n", + " \"\"\"\n", + " input = calculate_opt(\"-\"=[1,2,3,4,5])\n", + " returns 1-2-3-4-5\n", + "\n", + " input = calculate_opt(\"+\"=[1,2,3,4,5])\n", + " returns 1#\n", + "\n", + " \"\"\"\n", + " results = []\n", + " for operator, value in kwargs.items():\n", + " if operator == \"plus\":\n", + " results.append(addition(value))\n", + " elif operator == \"minus\":\n", + " results.append(subtraction(value))\n", + " elif operator == \"mult\": \n", + " results.append(multiplication(value))\n", + " elif operator == \"div\":\n", + " results.append(division(value))\n", + " else: \n", + " print(\"Wrong operator!\")\n", + " return results\n", + "\n", + "print(calculate_opt(plus = [1,2,3,4]))\n", + "print(calculate_opt(minus = [5, 1]))\n", + "print(calculate_opt(plus = [1,2,3,4], minus = [5, 1]))\n", + "print(calculate_opt(mult = [5, 1, 2]))\n", + "print(calculate_opt(div = [5, 1, 2]))\n", + "print(calculate_opt(mult = [5, 1, 2], div = [5, 1, 2]))\n", + "print(calculate_opt(plus = [1,2,3,4], minus = [5, 1], mult = [5, 1, 2], div = [5, 1, 2]))\n", + "print(calculate_opt(plus = [], minus = [], mult = [], div = []))" ] }, { @@ -273,16 +409,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 6, + "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\n", + "\n", + "\n", + "\n", + "functions.word_count_f(\"Note : this is an example !!! Good day : )\")" ] }, { @@ -315,18 +466,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n", + "34\n", + "21\n", + "13\n", + "8\n", + "5\n", + "3\n", + "2\n", + "1\n", + "1\n", + "0\n" + ] + } + ], "source": [ - "# your code goes here" + "def fibonacci(n):\n", + " if n == 0:\n", + " return 0\n", + " if n == 1:\n", + " return 1\n", + " else:\n", + " result = fibonacci(n-1) + fibonacci(n-2)\n", + " return result\n", + " \n", + "\n", + "def print_fib(n):\n", + " if(n == 0):\n", + " print(0)\n", + " return\n", + " print(fibonacci(n))\n", + " print_fib(n-1)\n", + "\n", + "\n", + "a = 10\n", + "fibonacci(a)\n", + "print_fib(a)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack_prework", "language": "python", "name": "python3" }, @@ -340,7 +529,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.14" } }, "nbformat": 4,