diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..f15c36f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -36,14 +36,9 @@ "def get_unique_list(lst):\n", " \"\"\"\n", " Takes a list as an argument and returns a new list with unique elements from the first list.\n", - "\n", - " Parameters:\n", - " lst (list): The input list.\n", - "\n", - " 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" ] }, { @@ -60,22 +55,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], "source": [ + "\n", "def count_case(string):\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", - "\n", - " Parameters:\n", - " string (str): The string to count uppercase and lowercase letters in.\n", - "\n", - " Returns:\n", - " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " upper = 0\n", + " lower = 0\n", + " \n", + " for char in string:\n", + " if char.isupper():\n", + " upper += 1\n", + " elif char.islower():\n", + " lower += 1\n", + " \n", + " return upper, lower\n" ] }, { @@ -92,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -101,28 +100,16 @@ "\n", "def remove_punctuation(sentence):\n", " \"\"\"\n", - " Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence.\n", - "\n", - " Parameters:\n", - " sentence (str): A string representing a sentence.\n", - "\n", - " Returns:\n", - " str: The sentence without any punctuation marks.\n", + " Removes all punctuation marks from a sentence.\n", " \"\"\"\n", - " # your code goes here\n", + " return sentence.translate(str.maketrans(\"\", \"\", string.punctuation))\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", - " 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.\n", - " \n", - " Parameters:\n", - " sentence (str): A string representing a sentence.\n", - "\n", - " Returns:\n", - " int: The number of words in the sentence.\n", + " Counts the number of words in a given sentence after removing punctuation.\n", " \"\"\"\n", - " # your code goes here" + " cleaned = remove_punctuation(sentence)\n", + " return len(cleaned.split())\n" ] }, { @@ -140,12 +127,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Wow This is great isnt it\n", + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "print(get_unique_list([1,2,3,3,3,3,4,5]))\n", + "print(count_case(\"Hello World\"))\n", + "print(remove_punctuation(\"Wow!!! This is great, isn't it?\"))\n", + "print(word_count(\"Note : this is an example !!! Good day : )\"))\n" ] }, { @@ -168,12 +169,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "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", + " if b == 0:\n", + " return \"Error: Division by zero\"\n", + " return b and 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" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6d5663d8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "6\n", + "12\n", + "Error: Division by zero\n" + ] + } + ], + "source": [ + "print(calculate(5, 3, \"+\"))\n", + "print(calculate(10, 4, \"-\"))\n", + "print(calculate(6, 2, \"*\"))\n", + "print(calculate(8, 0, \"/\"))\n" ] }, { @@ -192,12 +241,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def add_multi(*args):\n", + " return sum(args)\n", + "\n", + "def subtract_multi(*args):\n", + " result = args[0]\n", + " for n in args[1:]:\n", + " result -= n\n", + " return result\n", + "\n", + "def multiply_multi(*args):\n", + " result = 1\n", + " for n in args:\n", + " result *= n\n", + " return result\n", + "\n", + "def calculate_multi(operator, *args):\n", + " if operator == \"+\":\n", + " return add_multi(*args)\n", + " elif operator == \"-\":\n", + " return subtract_multi(*args)\n", + " elif operator == \"*\":\n", + " return multiply_multi(*args)\n", + " else:\n", + " return \"Invalid or unsupported operation\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d179621e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n", + "7\n", + "12\n" + ] + } + ], + "source": [ + "print(calculate_multi(\"+\", 2, 3, 4, 5)) # 14\n", + "print(calculate_multi(\"-\", 10, 2, 1)) # 7\n", + "print(calculate_multi(\"*\", 3, 2, 2)) # 12\n" ] }, { @@ -273,16 +367,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n", + "(2, 8)\n", + "Wow Amazing\n", + "4\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "from functions import *\n", + "\n", + "print(get_unique_list_f([1,1,2,3,4,4]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Wow!!! Amazing?\"))\n", + "print(word_count_f(\"Hey!!! This works?? Yes!\"))\n" ] }, { @@ -315,18 +425,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def fibonacci(n):\n", + " if n <= 1:\n", + " return 1\n", + " return fibonacci(n-1) + fibonacci(n-2)\n", + "\n", + "def fibonacci_sequence(n):\n", + " seq = []\n", + " for i in range(n):\n", + " seq.append(fibonacci(i))\n", + " return seq\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "ec82b8cf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], + "source": [ + "print(fibonacci_sequence(14))\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +477,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,