diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..5eda534 --- /dev/null +++ b/functions.py @@ -0,0 +1,57 @@ +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. + """ + return list(set(lst)) + + +def count_case_f(input_string): + """ + Returns the number of uppercase and lowercase letters in the given string. + + Parameters: + input_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. + """ + uppercase_count = sum(1 for char in input_string if char.isupper()) + lowercase_count = sum(1 for char in input_string if char.islower()) + return (uppercase_count, lowercase_count) + + +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. + """ + return sentence.translate(str.maketrans('', '', string.punctuation)) + + +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. + """ + clean_sentence = remove_punctuation_f(sentence) + return len(clean_sentence.split()) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..5e513d1 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.209148Z", + "iopub.status.busy": "2026-06-16T08:27:29.208993Z", + "iopub.status.idle": "2026-06-16T08:27:29.214934Z", + "shell.execute_reply": "2026-06-16T08:27:29.214135Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +58,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", + "# test\n", + "print(get_unique_list([1, 2, 3, 3, 3, 3, 4, 5]))\n" ] }, { @@ -60,10 +79,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.216938Z", + "iopub.status.busy": "2026-06-16T08:27:29.216401Z", + "iopub.status.idle": "2026-06-16T08:27:29.222080Z", + "shell.execute_reply": "2026-06-16T08:27:29.221260Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2, Lowercase count: 8\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +109,14 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " uppercase_count = sum(1 for char in string if char.isupper())\n", + " lowercase_count = sum(1 for char in string if char.islower())\n", + " return (uppercase_count, lowercase_count)\n", + "\n", + "\n", + "# test\n", + "upper, lower = count_case(\"Hello World\")\n", + "print(f\"Uppercase count: {upper}, Lowercase count: {lower}\")\n" ] }, { @@ -92,9 +133,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", - "metadata": {}, + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.224040Z", + "iopub.status.busy": "2026-06-16T08:27:29.223471Z", + "iopub.status.idle": "2026-06-16T08:27:29.227585Z", + "shell.execute_reply": "2026-06-16T08:27:29.226898Z" + } + }, "outputs": [], "source": [ "import string\n", @@ -109,7 +157,7 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " return sentence.translate(str.maketrans('', '', string.punctuation))\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +170,8 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " clean_sentence = remove_punctuation(sentence)\n", + " return len(clean_sentence.split())\n" ] }, { @@ -140,12 +189,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.229098Z", + "iopub.status.busy": "2026-06-16T08:27:29.228916Z", + "iopub.status.idle": "2026-06-16T08:27:29.232738Z", + "shell.execute_reply": "2026-06-16T08:27:29.231978Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "# test\n", + "print(remove_punctuation(\"Note : this is an example !!! Good day : )\"))\n", + "print(word_count(\"Note : this is an example !!! Good day : )\"))\n" ] }, { @@ -168,12 +235,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.234274Z", + "iopub.status.busy": "2026-06-16T08:27:29.234136Z", + "iopub.status.idle": "2026-06-16T08:27:29.240406Z", + "shell.execute_reply": "2026-06-16T08:27:29.239597Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "2\n", + "8\n", + "2.0\n" + ] + } + ], "source": [ - "# your code goes here" + "def add(a, b):\n", + " return a + b\n", + "\n", + "def subtract(a, b):\n", + " return a - b\n", + "\n", + "def multiply(a, b):\n", + " return a * b\n", + "\n", + "def divide(a, b):\n", + " return a / b\n", + "\n", + "def calculate(a, b, operator):\n", + " if operator == \"+\":\n", + " return add(a, b)\n", + " elif operator == \"-\":\n", + " return subtract(a, b)\n", + " elif operator == \"*\":\n", + " return multiply(a, b)\n", + " elif operator == \"/\":\n", + " return divide(a, b)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "\n", + "# test\n", + "print(calculate(4, 2, \"+\"))\n", + "print(calculate(4, 2, \"-\"))\n", + "print(calculate(4, 2, \"*\"))\n", + "print(calculate(4, 2, \"/\"))\n" ] }, { @@ -192,12 +306,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.242435Z", + "iopub.status.busy": "2026-06-16T08:27:29.241668Z", + "iopub.status.idle": "2026-06-16T08:27:29.248372Z", + "shell.execute_reply": "2026-06-16T08:27:29.247623Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n", + "24\n", + "10.0\n" + ] + } + ], "source": [ - "# your code goes here" + "def add(*args):\n", + " return sum(args)\n", + "\n", + "def subtract(*args):\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(*args):\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result /= num\n", + " return result\n", + "\n", + "def calculate(operator, *args):\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", + " return divide(*args)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "\n", + "# test\n", + "print(calculate(\"+\", 1, 2, 3, 4))\n", + "print(calculate(\"-\", 10, 2, 3))\n", + "print(calculate(\"*\", 1, 2, 3, 4))\n", + "print(calculate(\"/\", 100, 2, 5))\n" ] }, { @@ -260,22 +430,31 @@ "id": "14f222c5-e7bb-4626-b45c-bd735001f768", "metadata": {}, "source": [ - "To ensure that any changes made to the Python file are reflected in the Jupyter Notebook upon import, we need to use an IPython extension that allows for automatic reloading of modules. Without this extension, changes made to the file won't be reloaded or refreshed in the notebook upon import.\n", - "\n", - "For that, we will include the following code:\n", - "```python\n", + "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", - "```\n", "\n", - "You can read more about this here: https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html" + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f\n", + "\n", + "# test\n", + "print(get_unique_list_f([1, 2, 3, 3, 3, 3, 4, 5]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Note : this is an example !!! Good day : )\"))\n", + "print(word_count_f(\"Note : this is an example !!! Good day : )\"))\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", - "metadata": {}, + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.250074Z", + "iopub.status.busy": "2026-06-16T08:27:29.249906Z", + "iopub.status.idle": "2026-06-16T08:27:29.363784Z", + "shell.execute_reply": "2026-06-16T08:27:29.362872Z" + } + }, "outputs": [], "source": [ "# IPython extension to reload modules before executing user code.\n", @@ -315,12 +494,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", - "metadata": {}, - "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2026-06-16T08:27:29.365920Z", + "iopub.status.busy": "2026-06-16T08:27:29.365253Z", + "iopub.status.idle": "2026-06-16T08:27:29.374942Z", + "shell.execute_reply": "2026-06-16T08:27:29.373976Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10th Fibonacci number: 55\n", + "Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], "source": [ - "# your code goes here" + "def fibonacci_number(n):\n", + " \"\"\"\n", + " Recursively computes the nth Fibonacci number.\n", + " \"\"\"\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)\n", + "\n", + "def fibonacci_sequence(n):\n", + " \"\"\"\n", + " Generates a list of Fibonacci numbers from index 1 up to n.\n", + " \"\"\"\n", + " return [fibonacci_number(i) for i in range(1, n + 1)]\n", + "\n", + "\n", + "# test\n", + "print(f\"10th Fibonacci number: {fibonacci_number(10)}\")\n", + "print(f\"Fibonacci sequence: {fibonacci_sequence(14)}\")\n" ] } ], @@ -340,7 +555,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.3" } }, "nbformat": 4,