diff --git a/__pycache__/functions.cpython-314.pyc b/__pycache__/functions.cpython-314.pyc new file mode 100644 index 0000000..b4ce67c Binary files /dev/null and b/__pycache__/functions.cpython-314.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..2d9f7ba --- /dev/null +++ b/functions.py @@ -0,0 +1,89 @@ +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_numbers = [] + + for number in lst: + if number not in unique_numbers: + unique_numbers.append(number) + return unique_numbers + +lst = [1, 2, 2, 3, 4, 4, 5] +unique_numbers = get_unique_list_f(lst) +print(unique_numbers) + +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 + count_upper = 0 + count_lower = 0 + for char in string: + if char.isupper(): + count_upper += 1 + elif char.islower(): + count_lower += 1 + return count_upper, count_lower + +string = "Hello World!" +count_upper, count_lower = count_case_f(string) +print(f"Uppercase letters: {count_upper}, Lowercase letters: {count_lower}") + +import 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. + """ + # your code goes here + + replaced = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '') + return replaced + + +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 + + replaced = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '') + length = len(replaced.split()) + return length + +sentence = "Hello, is this an example sentence? Yes, it is!" +replaced = remove_punctuation_f(sentence) +print (replaced) + +sentence = "Hello, is this an example sentence? Yes, it is!" +length = word_count_f(sentence) +print (length) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..16e9a84 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -43,7 +43,34 @@ " 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", + " unique_numbers = []\n", + " \n", + " for number in lst:\n", + " if number not in unique_numbers:\n", + " unique_numbers.append(number)\n", + " return unique_numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "25b0c202", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "lst = [1, 2, 2, 3, 4, 4, 5]\n", + "unique_numbers = get_unique_list(lst)\n", + "print(unique_numbers) # Output: [1, 2, 3, 4" ] }, { @@ -60,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -75,7 +102,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", + " count_upper = 0\n", + " count_lower = 0\n", + " for char in string:\n", + " if char.isupper():\n", + " count_upper += 1\n", + " elif char.islower():\n", + " count_lower += 1\n", + " return count_upper, count_lower" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "2c91190c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase letters: 2, Lowercase letters: 8\n" + ] + } + ], + "source": [ + "string = \"Hello World!\"\n", + "count_upper, count_lower = count_case(string)\n", + "print(f\"Uppercase letters: {count_upper}, Lowercase letters: {count_lower}\")" ] }, { @@ -92,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -111,6 +166,10 @@ " \"\"\"\n", " # your code goes here\n", "\n", + " replaced = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '')\n", + " return replaced\n", + "\n", + "\n", "def word_count(sentence):\n", " \"\"\"\n", " Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence.\n", @@ -122,7 +181,32 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " \n", + " replaced = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '')\n", + " length = len(replaced.split())\n", + " return length\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64837152", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello is this an example sentence Yes it is\n", + "9\n" + ] + } + ], + "source": [ + "sentence = \"Hello, is this an example sentence? Yes, it is!\"\n", + "replaced = remove_punctuation(sentence)\n", + "print (replaced)" ] }, { @@ -140,12 +224,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "sentence = \"Hello, is this an example sentence? Yes, it is!\"\n", + "length = word_count(sentence)\n", + "print (length)" ] }, { @@ -168,12 +264,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def test_add(a, b):\n", + " result = a + b\n", + " return result\n", + "\n", + "def test_subtract(a, b):\n", + " result = a - b\n", + " return result \n", + "\n", + "def test_multiply(a, b):\n", + " result = a * b\n", + " return result\n", + "\n", + "def test_divide(a, b):\n", + " result = a / b\n", + " return result\n", + "def calculate(a, b, operation):\n", + "\n", + " if operation == \"add\":\n", + " return test_add(a, b)\n", + " if operation == \"subtract\":\n", + " return test_subtract(a, b)\n", + " if operation == \"multiply\":\n", + " return test_multiply(a, b)\n", + " if operation == \"divide\":\n", + " return test_divide(a, b)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "2c8207b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], + "source": [ + "test = calculate(10, 5, \"add\")\n", + "print(test)\n", + "test = calculate(10, 5, \"subtract\")\n", + "print(test)\n", + "test = calculate(10, 5, \"multiply\")\n", + "print(test)\n", + "test = calculate(10, 5, \"divide\")\n", + "print(test)" ] }, { @@ -192,12 +342,72 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def test_add(*args):\n", + " return sum(args)\n", + "\n", + "def test_subtract(*args):\n", + " result = 0\n", + " for num in args:\n", + " result -= num\n", + " return result\n", + "\n", + "def test_multiply(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "def test_divide(*args):\n", + " result = 0\n", + " for num in args:\n", + " result /= num\n", + " return result\n", + "\n", + "def calculate(*args, operation):\n", + "\n", + " if operation == \"add\":\n", + " return test_add(*args)\n", + " if operation == \"subtract\":\n", + " return test_subtract(*args)\n", + " if operation == \"multiply\":\n", + " return test_multiply(*args)\n", + " if operation == \"divide\":\n", + " return test_divide(*args)" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "e88f4ed7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n", + "-115\n", + "500\n", + "0.0\n" + ] + } + ], + "source": [ + "test = calculate(10, 5, 10, operation=\"add\")\n", + "print(test)\n", + "test = calculate(100, 5, 10, operation=\"subtract\")\n", + "print(test)\n", + "test = calculate(10, 5, 10, operation=\"multiply\")\n", + "print(test)\n", + "test = calculate(10, 5, 10, operation=\"divide\")\n", + "print(test)" ] }, { @@ -273,16 +483,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "Uppercase letters: 2, Lowercase letters: 8\n", + "Hello is this an example sentence Yes it is\n", + "9\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "# your code goes here\n", + "\n", + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f\n" ] }, { @@ -320,13 +543,16 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + " \n", + " \n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +566,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,