diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..4c9bd6f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "['apple', 'banana', 'orange']\n", + "[]\n", + "[1]\n" + ] + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +54,26 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " unique_list = []\n", + " for item in lst:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list\n", + "\n", + "\n", + "# Test cases\n", + "print(get_unique_list([1, 2, 2, 3, 4, 4, 5]))\n", + "# Expected: [1, 2, 3, 4, 5]\n", + "\n", + "print(get_unique_list([\"apple\", \"banana\", \"apple\", \"orange\", \"banana\"]))\n", + "# Expected: ['apple', 'banana', 'orange']\n", + "\n", + "print(get_unique_list([]))\n", + "# Expected: []\n", + "\n", + "print(get_unique_list([1, 1, 1, 1]))\n", + "# Expected: [1]\n", + "\n" ] }, { @@ -63,7 +93,18 @@ "execution_count": null, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 8)\n", + "(6, 11)\n", + "(0, 0)\n", + "(3, 3)\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,9 +116,40 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " upper_count = 0\n", + " lower_count = 0\n", + "\n", + " for char in string:\n", + " if char.isupper():\n", + " upper_count += 1\n", + " elif char.islower():\n", + " lower_count += 1\n", + "\n", + " return (upper_count, lower_count)\n", + "\n", + "\n", + "# Test cases\n", + "print(count_case(\"Hello World\"))\n", + "# Expected: (2, 8)\n", + "\n", + "print(count_case(\"PYTHON programming\"))\n", + "# Expected: (6, 11)\n", + "\n", + "print(count_case(\"1234!@#\"))\n", + "# Expected: (0, 0)\n", + "\n", + "print(count_case(\"aBcDeF\"))\n", + "# Expected: (3, 3)\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5dde7e1", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "2c902b2e-369c-4a40-a166-ca5bc554cab3", @@ -92,10 +164,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world\n", + "Wait What\n", + "2\n", + "3\n", + "2\n" + ] + } + ], "source": [ "import string\n", "\n", @@ -109,7 +193,15 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " punctuation_marks = \",.!?\"\n", + " \n", + " cleaned_sentence = \"\"\n", + " for char in sentence:\n", + " if char not in punctuation_marks:\n", + " cleaned_sentence += char\n", + " \n", + " return cleaned_sentence\n", + "\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +214,26 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " cleaned_sentence = remove_punctuation(sentence)\n", + " words = cleaned_sentence.split(\" \")\n", + " return len(words)\n", + "\n", + "\n", + "# Test cases\n", + "print(remove_punctuation(\"Hello, world!\"))\n", + "# Expected: \"Hello world\"\n", + "\n", + "print(remove_punctuation(\"Wait... What?!\"))\n", + "# Expected: \"Wait What\"\n", + "\n", + "print(word_count(\"Hello, world!\"))\n", + "# Expected: 2\n", + "\n", + "print(word_count(\"Python is awesome.\"))\n", + "# Expected: 3\n", + "\n", + "print(word_count(\"Wait... What?!\"))\n", + "# Expected: 2\n" ] }, { @@ -140,12 +251,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "import string\n", + "\n", + "def remove_punctuation(sentence):\n", + " \"\"\"\n", + " Removes all punctuation 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", + " \"\"\"\n", + " cleaned_sentence = \"\"\n", + " \n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " cleaned_sentence += char\n", + " \n", + " return cleaned_sentence\n", + "\n", + "\n", + "def word_count(sentence):\n", + " \"\"\"\n", + " Counts the number of words in a given sentence. \n", + " First removes punctuation from the 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", + " \"\"\"\n", + " cleaned_sentence = remove_punctuation(sentence)\n", + " words = cleaned_sentence.split()\n", + " return len(words)\n", + "\n", + "\n", + "# Test case from your example\n", + "print(word_count(\"Note : this is an example !!! Good day : )\"))\n", + "# Expected output: 7\n" ] }, { @@ -168,12 +326,70 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n", + "Error: Division by zero is not allowed.\n", + "Error: Invalid operator.\n" + ] + } + ], "source": [ - "# your code goes here" + "# Arithmetic functions\n", + "\n", + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def subtract(a, b):\n", + " return a - b\n", + "\n", + "\n", + "def multiply(a, b):\n", + " return a * b\n", + "\n", + "\n", + "def divide(a, b):\n", + " if b == 0:\n", + " return \"Error: Division by zero is not allowed.\"\n", + " return a / b\n", + "\n", + "\n", + "# Main calculator function\n", + "\n", + "def calculate(a, b, operator):\n", + " \"\"\"\n", + " Takes two operands and an operator, then performs\n", + " the corresponding arithmetic operation.\n", + " \"\"\"\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 \"Error: Invalid operator.\"\n", + "\n", + "\n", + "# Test cases\n", + "print(calculate(10, 5, \"+\")) # Expected: 15\n", + "print(calculate(10, 5, \"-\")) # Expected: 5\n", + "print(calculate(10, 5, \"*\")) # Expected: 50\n", + "print(calculate(10, 5, \"/\")) # Expected: 2.0\n", + "print(calculate(10, 0, \"/\")) # Expected: Error message\n", + "print(calculate(10, 5, \"%\")) # Expected: Invalid operator error\n" ] }, { @@ -192,12 +408,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n", + "24\n", + "4.0\n", + "Error: Division requires exactly two numbers.\n", + "100\n" + ] + } + ], "source": [ - "# your code goes here" + "# Arithmetic functions that accept multiple numbers\n", + "\n", + "def add(*args):\n", + " total = 0\n", + " for num in args:\n", + " total += num\n", + " return total\n", + "\n", + "\n", + "def subtract(*args):\n", + " if len(args) == 0:\n", + " return 0\n", + " \n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "\n", + "def multiply(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "\n", + "def divide(a, b):\n", + " if b == 0:\n", + " return \"Error: Division by zero is not allowed.\"\n", + " return a / b\n", + "\n", + "\n", + "# Updated calculate function\n", + "\n", + "def calculate(operator, *args):\n", + " \"\"\"\n", + " Takes an operator and multiple operands,\n", + " then performs the corresponding arithmetic operation.\n", + " \"\"\"\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", + " if len(args) != 2:\n", + " return \"Error: Division requires exactly two numbers.\"\n", + " return divide(args[0], args[1])\n", + " else:\n", + " return \"Error: Invalid operator.\"\n", + "\n", + "\n", + "# Test cases\n", + "print(calculate(\"+\", 1, 2, 3, 4)) # Expected: 10\n", + "print(calculate(\"-\", 10, 2, 3)) # Expected: 5 (10 - 2 - 3)\n", + "print(calculate(\"*\", 2, 3, 4)) # Expected: 24\n", + "print(calculate(\"/\", 20, 5)) # Expected: 4.0\n", + "print(calculate(\"/\", 20, 5, 2)) # Expected: Error\n", + "print(calculate(\"+\", 100)) # Expected: 100\n" ] }, { @@ -315,18 +603,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10th Fibonacci number: 55\n", + "Fibonacci sequence (n=14):\n", + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n", + "Fibonacci sequence (n=5):\n", + "[1, 1, 2, 3, 5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# Function to compute the nth Fibonacci number using recursion\n", + "def fibonacci(n):\n", + " \"\"\"\n", + " Returns the nth Fibonacci number using recursion.\n", + " \"\"\"\n", + " if n <= 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " else:\n", + " return fibonacci(n - 1) + fibonacci(n - 2)\n", + "\n", + "\n", + "# Function to generate Fibonacci sequence up to n terms\n", + "def generate_fibonacci_sequence(n):\n", + " \"\"\"\n", + " Generates a list of Fibonacci numbers from 1 to n terms.\n", + " \"\"\"\n", + " sequence = []\n", + " \n", + " for i in range(1, n + 1):\n", + " sequence.append(fibonacci(i))\n", + " \n", + " return sequence\n", + "\n", + "\n", + "# ✅ Test cases\n", + "print(\"10th Fibonacci number:\", fibonacci(10)) \n", + "# Expected: 55\n", + "\n", + "print(\"Fibonacci sequence (n=14):\")\n", + "print(generate_fibonacci_sequence(14))\n", + "# Expected:\n", + "# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n", + "\n", + "print(\"Fibonacci sequence (n=5):\")\n", + "print(generate_fibonacci_sequence(5))\n", + "# Expected: [1, 1, 2, 3, 5]\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -340,7 +677,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,