diff --git a/__pycache__/functions.cpython-312.pyc b/__pycache__/functions.cpython-312.pyc new file mode 100644 index 0000000..4db41bd Binary files /dev/null and b/__pycache__/functions.cpython-312.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..42dce41 --- /dev/null +++ b/functions.py @@ -0,0 +1,79 @@ + +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 + unique_list = [] + for item in lst: + if item not in unique_list: # uniqueness check: if the item is already in unique_list, we skip it; if it’s not there yet, we accept it + unique_list.append(item) + return unique_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 + upper_count = 0 + lower_count = 0 + for char in string: + if char.isupper(): + upper_count += 1 + elif char.islower(): + lower_count += 1 + return (upper_count, lower_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. + """ + # your code goes here + punctuation_marks = string.punctuation # Get all standard punctuation marks + no_punct_sentence = ''.join(char for char in sentence if char not in punctuation_marks) + return no_punct_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. + """ + # your code goes here + no_punct_sentence = remove_punctuation_f(sentence) + words = no_punct_sentence.split() # Split the sentence into words based on spaces + return len(words) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..60df728 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,7 +43,12 @@ " 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", + " unique_list = []\n", + " for item in lst:\n", + " if item not in unique_list: # uniqueness check: if the item is already in unique_list, we skip it; if it’s not there yet, we accept it\n", + " unique_list.append(item)\n", + " return unique_list" ] }, { @@ -58,6 +63,17 @@ "*Expected Output [1,2,3,4,5] -> type: list*" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "53952118", + "metadata": {}, + "outputs": [], + "source": [ + "# Example Test: Integers with duplicates\n", + "print(get_unique_list([1,2,3,3,3,3,4,5]))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -75,7 +91,15 @@ " 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_count = 0\n", + " lower_count = 0\n", + " for char in string:\n", + " if char.isupper():\n", + " upper_count += 1\n", + " elif char.islower():\n", + " lower_count += 1\n", + " return (upper_count, lower_count)" ] }, { @@ -90,6 +114,17 @@ "*Expected Output: Uppercase count: 2, Lowercase count: 8* " ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "e77bdaa9", + "metadata": {}, + "outputs": [], + "source": [ + "# Example Test: String input\n", + "print(count_case(\"Hello World!\"))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -110,6 +145,9 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " punctuation_marks = string.punctuation # Get all standard punctuation marks\n", + " no_punct_sentence = ''.join(char for char in sentence if char not in punctuation_marks)\n", + " return no_punct_sentence\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +160,10 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " no_punct_sentence = remove_punctuation(sentence)\n", + " words = no_punct_sentence.split() # Split the sentence into words based on spaces\n", + " return len(words)" ] }, { @@ -145,7 +186,7 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "print(word_count(\"Note : this is an example !!! Good day : )\")) # Example usage" ] }, { @@ -173,7 +214,41 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# define four arithmetic functions\n", + "def add(a, b):\n", + " return a + b\n", + "def subtract(a, b):\n", + " return a - b\n", + "def multiply(a, b):\n", + " return a * b\n", + "def divide(a, b):\n", + " return a / b\n", + "\n", + "# define a function called \"calculate\"\n", + "def calculate(a, b, operator):\n", + " if operator == 'add':\n", + " return add(a, b)\n", + " elif operator == 'subtract':\n", + " return subtract(a, b)\n", + " elif operator == 'multiply':\n", + " return multiply(a, b)\n", + " elif operator == 'divide':\n", + " return divide(a, b)\n", + " else:\n", + " raise ValueError(\"Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8ed904c7", + "metadata": {}, + "outputs": [], + "source": [ + "print(calculate(10, 5, 'add')) # Output: 15\n", + "print(calculate(10, 5, 'subtract')) # Output: 5\n", + "print(calculate(10, 5, 'multiply')) # Output: 50\n", + "print(calculate(10, 5, 'divide')) # Output: 2." ] }, { @@ -192,12 +267,79 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", + "execution_count": 1, + "id": "edf88ab8", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def add(*args):\n", + " if not args:\n", + " raise ValueError(\"At least one number is required.\")\n", + " return sum(args)\n", + "\n", + "def multiply(*args):\n", + " if not args:\n", + " raise ValueError(\"At least one number is required.\")\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "def subtract(*args):\n", + " if len(args) < 2:\n", + " raise ValueError(\"Subtraction requires at least two numbers.\")\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "def divide(*args):\n", + " if len(args) < 2:\n", + " raise ValueError(\"Division requires at least two numbers.\")\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " if num == 0:\n", + " raise ZeroDivisionError(\"Cannot divide by zero.\")\n", + " result /= num\n", + " return result\n", + "\n", + "def calculate(*args, operator):\n", + " if operator == 'add':\n", + " return add(*args)\n", + " elif operator == 'subtract':\n", + " return subtract(*args)\n", + " elif operator == 'multiply':\n", + " return multiply(*args)\n", + " elif operator == 'divide':\n", + " return divide(*args)\n", + " else:\n", + " raise ValueError(\n", + " \"Invalid operation. Choose from 'add', 'subtract', 'multiply', or 'divide'.\"\n", + " )\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "44ef0364", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n", + "24\n", + "10.0\n" + ] + } + ], + "source": [ + "print(calculate(1, 2, 3, 4, operator='add')) # 10\n", + "print(calculate(10, 2, 3, operator='subtract')) # 5\n", + "print(calculate(2, 3, 4, operator='multiply')) # 24\n", + "print(calculate(100, 2, 5, operator='divide')) # 10.0" ] }, { @@ -274,15 +416,104 @@ { "cell_type": "code", "execution_count": null, - "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", + "id": "a05f729d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", - "%autoreload 2 \n", + "%autoreload 2 " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Hello world How are you\n", + "5\n" + ] + } + ], + "source": [ + "# 1. Importing specific functions: from function_file import function_name\n", + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f\n", "\n", - "# your code goes here" + "# Test get_unique_list_f\n", + "print(get_unique_list_f([1, 2, 2, 3, 4, 4, 5]))\n", + "\n", + "# Test count_case_f\n", + "print(count_case_f(\"Hello World!\"))\n", + "\n", + "# Test remove_punctuation_f\n", + "print(remove_punctuation_f(\"Hello, world! How are you?\"))\n", + "\n", + "# Test word_count_f\n", + "print(word_count_f(\"Hello, world! How are you?\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "369b614b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30]\n", + "(5, 6)\n", + "Wow This is great\n", + "4\n" + ] + } + ], + "source": [ + "# 2. Importing the entire module\n", + "import functions\n", + "\n", + "print(functions.get_unique_list_f([10, 10, 20, 30, 30]))\n", + "print(functions.count_case_f(\"Python Is FUN\"))\n", + "print(functions.remove_punctuation_f(\"Wow!!! This is great.\"))\n", + "print(functions.word_count_f(\"Wow!!! This is great.\"))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3c62aa16", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "# 3. Renaming functions during import\n", + "from functions import word_count_f as wc\n", + "\n", + "print(wc(\"This sentence has five words.\"))\n" ] }, { @@ -326,7 +557,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +571,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,