From 4280e6491c287e73d4d280fa61f61dc068bb57ce Mon Sep 17 00:00:00 2001 From: Prabhsheenkaur Date: Sat, 11 Apr 2026 11:21:50 +0100 Subject: [PATCH] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 204 ++++++++++++++++++++++++++++++++----- 1 file changed, 178 insertions(+), 26 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..79611b1 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" + " return list(set(lst))\n", + "\n", + "\n", + "# Test\n", + "numbers = [1, 2, 2, 3, 4, 4, 5]\n", + "print(get_unique_list(numbers))" ] }, { @@ -75,7 +80,20 @@ " 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\n", + "print(count_case(\"Hello World\"))" ] }, { @@ -102,27 +120,27 @@ "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", " \"\"\"\n", - " # your code goes here\n", + " result = \"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result += char\n", + " return result\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", - " 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.\n", " \"\"\"\n", - " # your code goes here" + " clean_sentence = remove_punctuation(sentence)\n", + " words = clean_sentence.split()\n", + " return len(words)\n", + "\n", + "\n", + "# Test\n", + "text = \"Hello, world! This is a test.\"\n", + "print(remove_punctuation(text)) # \"Hello world This is a test\"\n", + "print(word_count(text)) # 6" ] }, { @@ -145,7 +163,7 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "print(word_count(\"Note : this is an example !!! Good day : )\"))" ] }, { @@ -173,7 +191,44 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def subtraction(a, b):\n", + " return a - b\n", + "\n", + "\n", + "def multiplication(a, b):\n", + " return a * b\n", + "\n", + "\n", + "def division(a, b):\n", + " if b == 0:\n", + " return \"Cannot divide by zero\"\n", + " return a / b\n", + "\n", + "\n", + "def calculate(a, b, operator):\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", + "\n", + "# Tests\n", + "print(calculate(10, 5, \"+\")) # 15\n", + "print(calculate(10, 5, \"-\")) # 5\n", + "print(calculate(10, 5, \"*\")) # 50\n", + "print(calculate(10, 5, \"/\")) # 2.0\n", + "print(calculate(10, 0, \"/\")) # Cannot divide by zero\n", + "print(calculate(10, 5, \"%\")) # Invalid operator" ] }, { @@ -197,7 +252,56 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(*args):\n", + " return sum(args)\n", + "\n", + "\n", + "def subtraction(*args):\n", + " if len(args) == 0:\n", + " return 0\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "\n", + "def multiplication(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "\n", + "def division(*args):\n", + " if len(args) == 0:\n", + " return 0\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " if num == 0:\n", + " return \"Cannot divide by zero\"\n", + " result /= num\n", + " return result\n", + "\n", + "\n", + "def calculate(operator, *args):\n", + " if operator == \"+\":\n", + " return addition(*args)\n", + " elif operator == \"-\":\n", + " return subtraction(*args)\n", + " elif operator == \"*\":\n", + " return multiplication(*args)\n", + " elif operator == \"/\":\n", + " return division(*args)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "\n", + "# Tests\n", + "print(calculate(\"+\", 1, 2, 3, 4)) # 10\n", + "print(calculate(\"-\", 10, 2, 3)) # 5\n", + "print(calculate(\"*\", 2, 3, 4)) # 24\n", + "print(calculate(\"/\", 100, 2, 5)) # 10.0\n", + "print(calculate(\"/\", 10, 0)) # Cannot divide by zero" ] }, { @@ -278,11 +382,42 @@ "metadata": {}, "outputs": [], "source": [ - "# IPython extension to reload modules before executing user code.\n", - "%load_ext autoreload\n", - "%autoreload 2 \n", + "import string\n", + "\n", + "\n", + "def get_unique_list_f(lst):\n", + " unique = []\n", + " for item in lst:\n", + " if item not in unique:\n", + " unique.append(item)\n", + " return unique\n", + "\n", "\n", - "# your code goes here" + "def count_case_f(text):\n", + " upper_count = 0\n", + " lower_count = 0\n", + "\n", + " for char in text:\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", + "def remove_punctuation_f(sentence):\n", + " result = \"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result += char\n", + " return result\n", + "\n", + "\n", + "def word_count_f(sentence):\n", + " clean_sentence = remove_punctuation_f(sentence)\n", + " words = clean_sentence.split()\n", + " return len(words)" ] }, { @@ -320,7 +455,24 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# Recursive function to compute nth Fibonacci number\n", + "def fibonacci(n):\n", + " if n <= 1:\n", + " return n\n", + " return fibonacci(n - 1) + fibonacci(n - 2)\n", + "\n", + "\n", + "# Function to generate Fibonacci sequence from 1 to n\n", + "def fibonacci_sequence(n):\n", + " sequence = []\n", + " for i in range(1, n + 1):\n", + " sequence.append(fibonacci(i))\n", + " return sequence\n", + "\n", + "\n", + "# Test\n", + "n = 14\n", + "print(\"Fibonacci sequence:\", fibonacci_sequence(n))" ] } ],