diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000..79a5aeb Binary files /dev/null and b/__pycache__/functions.cpython-313.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..4be4b6c --- /dev/null +++ b/functions.py @@ -0,0 +1,71 @@ +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 + new_list = list(set(lst)) + return new_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 + up = 0 + low = 0 + for char in string: + if char.isupper(): + up += 1 + elif char.islower(): + low += 1 + t = (up, low) + print(f"Uppercase count: {up}, Lowercase count: {low}") + return t + + +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 + new_string = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '').replace(':', '') + print(new_string) + return new_string + +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 + cleaned_sentence = remove_punctuation_f(sentence) + words = cleaned_sentence.split() + count = len(words) + print(f"Word count: {count}") + return count + + diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..b7ad30a 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 2, + "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" + " # your code goes here\n", + " new_list = list(set(lst))\n", + " return new_list\n", + "\n", + "get_unique_list([1, 2, 2, 3, 4, 4, 5])" ] }, { @@ -63,8 +78,29 @@ "execution_count": null, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase letters: 2, Lowercase letters: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "(2, 8)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "import string\n", + "\n", + "\n", "def count_case(string):\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", @@ -75,7 +111,19 @@ " 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", + " up = 0\n", + " low = 0\n", + " for char in string:\n", + " if char.isupper():\n", + " up += 1\n", + " elif char.islower():\n", + " low += 1\n", + " t = (up, low)\n", + " print(f\"Uppercase count: {up}, Lowercase count: {low}\")\n", + " return t\n", + "\n", + "count_case(\"Hello World!\")" ] }, { @@ -92,10 +140,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "Word count: 7\n" + ] + }, + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import string\n", "\n", @@ -110,6 +177,9 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " new_string = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '').replace(':', '')\n", + " print(new_string)\n", + " return new_string\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +192,16 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " cleaned_sentence = remove_punctuation(sentence)\n", + " words = cleaned_sentence.split()\n", + " count = len(words)\n", + " print(f\"Word count: {count}\")\n", + " return count\n", + "\n", + "\n", + "#remove_punctuation(\"Note : this is an example !!! Good day : \")\n", + "word_count(\"Note : this is an example !!! Good day : \")\n" ] }, { @@ -168,12 +247,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "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 calculation(operator, a, b): \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", + " return \"Invalid operator\"\n", + "\n", + "print(calculation('+', 10, 5))\n", + "print(calculation('-', 10, 5))\n", + "print(calculation('*', 10, 5))\n", + "print(calculation('/', 10, 5))" ] }, { @@ -192,12 +310,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17\n", + "3\n", + "100\n", + "1.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def addition(*args):\n", + " return sum(args)\n", + "\n", + "def subtraction(*args):\n", + " for arg in args:\n", + " if arg == args[0]:\n", + " result = arg\n", + " else:\n", + " result -= arg\n", + " return result\n", + "\n", + "def multiplication(*args):\n", + " for arg in args:\n", + " if 'result' not in locals():\n", + " result = arg\n", + " else:\n", + " result *= arg\n", + " return result\n", + "\n", + "def division(*args):\n", + " for arg in args:\n", + " if arg == args[0]:\n", + " result = arg\n", + " else:\n", + " result /= arg\n", + " return result\n", + "\n", + "\n", + "print(addition(10, 5, 2))\n", + "print(subtraction(10, 5, 2))\n", + "print(multiplication(10, 5, 2))\n", + "print(division(10, 5, 2))" ] }, { @@ -255,6 +416,42 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "227913fc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world This is a test\n", + "Hello world This is a test\n", + "Word count: 6\n", + "Uppercase count: 2, Lowercase count: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "(2, 8)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import functions\n", + "\n", + "functions.remove_punctuation_f(\"Hello, world! This is a test.\")\n", + "functions.word_count_f(\"Hello, world! This is a test.\")\n", + "functions.get_unique_list_f([1, 2, 2, 3, 4, 4, 5])\n", + "functions.count_case_f(\"Hello World!\")\n" + ] + }, { "cell_type": "markdown", "id": "14f222c5-e7bb-4626-b45c-bd735001f768", @@ -315,18 +512,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def fib(n):\n", + " if n == 0:\n", + " return [0]\n", + " if n == 1:\n", + " return [0,1]\n", + " seq = fib(n-1)\n", + " seq.append(seq[-1] + (seq[-2]))\n", + " return seq\n", + "\n", + "fib(14)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +558,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,