diff --git a/main.ipynb b/main.ipynb index b05630a..9750d23 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -37,31 +37,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 138, "metadata": {}, "outputs": [], "source": [ - "def greater(a,b):\n", - "#your code here" + "def greater_number(a, b):\n", + " \"\"\"Return the greater of two numbers.\"\"\"\n", + " if a > b:\n", + " return a\n", + " else:\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "metadata": {}, "outputs": [], "source": [ - "# This will test your function \n", - "test_greater(greater)" + "def test_greater(func):\n", + " print(greater_number(10, 5)) # Output: 10\n", + " print(greater_number(3, 8)) # Output: 8" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 140, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "8\n" + ] + } + ], "source": [ - "#your code here" + "# This will test your function \n", + "test_greater(greater_number)" ] }, { @@ -73,21 +88,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "def largest_element(lst):\n", + " \"\"\"Return the largest element in a list.\"\"\"\n", + " if not lst:\n", + " return None\n", + " largest = lst[0]\n", + " for num in lst:\n", + " if num > largest:\n", + " largest = num\n", + " return largest\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], + "source": [ + "def test_largest(func):\n", + " print(largest_element([3, 1, 4, 1, 5, 9, 2, 6, 5])) # Output: 9\n", + " print(largest_element([])) # Output: None" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n", + "None\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_greatest(greatest)" + "test_largest(largest_element)" ] }, { @@ -99,21 +142,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sum of numbers: 36\n" + ] + } + ], "source": [ - "def sum_all(lst):\n", - "#your code here" + "numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]\n", + "sum_result = 0\n", + "for number in numbers:\n", + " sum_result += number\n", + "print(\"Sum of numbers:\", sum_result)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.104s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -128,21 +194,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "def mult_all(lst):\n", - "#your code here" + " \"\"\"Return the product of all elements in a list.\"\"\"\n", + " if not lst:\n", + " return None\n", + " product = 1\n", + " for num in lst:\n", + " product *= num\n", + " return product\n", + "def test_mult(func):\n", + " print(mult_all([1, 2, 3, 4])) # Output: 24\n", + " print(mult_all([])) # Output: None" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "24\n", + "None\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -157,22 +241,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ - "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "def calculate(numbers, operation):\n", + " \"\"\"perform the specified operation sum or product on the list of numbers\"\"\"\n", + " if operation == 'sum':\n", + " return sum(numbers)\n", + " elif operation == 'product':\n", + " return mult_all(numbers)\n", + " else:\n", + " return None\n", + "def test_calculate(func):\n", + " print(calculate([1, 2, 3, 4], 'sum')) # Output: 10\n", + " print(calculate([1, 2, 3, 4], 'product')) # Output: 24\n", + " print(calculate([1, 2, 3, 4], 'average')) # Output: None" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "24\n", + "None\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_operations(oper_all)" + "test_calculate(calculate)" ] }, { @@ -184,17 +288,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", - "#your code here" + " \"\"\"Return the factorial of a number.\"\"\"\n", + " print(n, '!')\n", + " # Corner case: 0! and negative numbers both return 1\n", + " if n <= 0:\n", + " return 1\n", + " else:\n", + " # Recursive case: n! = n * (n-1)!\n", + " result = n * factorial(n - 1)\n", + " return result\n", + "\n", + "\n", + "def test_factorial(func):\n", + " print(func(5)) # Expected output: 120\n", + " print(func(0)) # Expected output: 1\n", + " print(func(-3)) # Expected output: 1" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, "outputs": [], "source": [ @@ -213,9 +331,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 !\n", + "4 !\n", + "3 !\n", + "2 !\n", + "1 !\n", + "0 !\n", + "120\n", + "0 !\n", + "1\n", + "-3 !\n", + "1\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -232,19 +368,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "def unique(lst_un):\n", - "#your code here" + " \"\"\"Return a list of unique elements from the input list.\"\"\"\n", + " unique_list = []\n", + " for item in lst_un:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list\n", + "def test_unique(func):\n", + " print(unique([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]\n", + " print(unique([])) # Output: []" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "[]\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -260,19 +413,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, "outputs": [], "source": [ - "def mode_counter(arr):\n", - "#your code here" + "def find_mode(numbers):\n", + " \"\"\"Return the mode of a list (the element that appears most often).\"\"\"\n", + " \n", + " if not numbers:\n", + " return None # Handle empty list\n", + " \n", + " # Step 1: Count occurrences using a dictionary\n", + " frequency = {} # empty dictionary\n", + " for num in numbers:\n", + " if num in frequency:\n", + " frequency[num] += 1 # increment count\n", + " else:\n", + " frequency[num] = 1 # first occurrence\n", + " \n", + " # Step 2: Find the number with the maximum count\n", + " mode = None\n", + " max_count = 0\n", + " for num, count in frequency.items():\n", + " if count > max_count:\n", + " max_count = count\n", + " mode = num\n", + " return mode\n", + "def test_mode(func):\n", + " print(mode([1, 2, 3, 4, 5, 5, 5])) # Output: 2\n", + " print(mode([1, 1, 2, 2, 3])) # Output: 1 or 2 (any one of the modes)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "1\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -288,19 +473,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "def st_dev(list_sd):\n", - "#your code here" + " \"\"\"Return the standard deviation of a list of numbers.\"\"\"\n", + " if not list_sd:\n", + " return None # Handle empty list\n", + " \n", + " n = len(list_sd)\n", + " mean = sum(list_sd) / n\n", + " squared_diffs = [(x - mean) ** 2 for x in list_sd]\n", + " variance = sum((x - mean) ** 2 for x in list_sd) / n\n", + " std_deviation = variance ** 0.5\n", + " return std_deviation\n", + "def test_stdev(func):\n", + " print(st_dev([1, 2, 3, 4, 5])) # Output: 1.4142135623730951\n", + " print(st_dev([])) # Output: None" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.4142135623730951\n", + "None\n" + ] + } + ], "source": [ "# This will test your function \n", "test_stdev(st_dev)" @@ -317,20 +523,61 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [ - "def pangram(string):\n", - "#your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "def is_pangram(s):\n", + " # Step 1: convert the string to lowercase\n", + " s = s.lower()\n", + " \n", + " # Step 2: create a set to store letters we encounter\n", + " letters_seen = set()\n", + " \n", + " # Step 3: iterate through each character in the string\n", + " for char in s:\n", + " # Step 4: check if the character is a letter\n", + " if char.isalpha(): # True if char is a-z or A-Z\n", + " letters_seen.add(char)\n", + " \n", + " # Step 5: check if we have all 26 letters\n", + " return len(letters_seen) == 26\n", + "\n", + "# Example usage:\n", + "sentence = \"The quick brown fox jumps over the lazy dog\"\n", + "print(is_pangram(sentence)) # Output: True\n", + "\n", + "sentence2 = \"Hello World\"\n", + "print(is_pangram(sentence2)) # Output: False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.056s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_pangram(pangram)" + "test_pangram(is_pangram)" ] }, { @@ -344,22 +591,60 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 129, "metadata": {}, "outputs": [], "source": [ - "def sort_alpha(string):\n", - "#your code here" + "def sort_words(text):\n", + " words = [] # to store words we extract\n", + " current_word = \"\" # to build each word letter by letter\n", + " \n", + " # Step 1: manually split the string by commas\n", + " for ch in text:\n", + " if ch == ',':\n", + " words.append(current_word)\n", + " current_word = \"\" # reset for next word\n", + " else:\n", + " current_word += ch # build the word\n", + " \n", + " # Don't forget to add the last word after the loop\n", + " words.append(current_word)\n", + "\n", + " # Step 2: sort the list of words\n", + " sorted_words = sorted(words)\n", + " \n", + " # Step 3: manually build the result string (no join!)\n", + " result = \"\"\n", + " for i in range(len(sorted_words)):\n", + " result += sorted_words[i]\n", + " if i < len(sorted_words) - 1: # add comma except after last word\n", + " result += ','\n", + " \n", + " # Step 4: return the final string\n", + " return result\n", + "\n", + "def test_sort(func):\n", + " print(sort_words(\"banana,apple,cherry\")) # Output: \"apple,banana,cherry\"\n", + " print(sort_words(\"dog,cat,bird\")) # Output: \"bird,cat,dog\"" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 128, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple,banana,cherry\n", + "bird,cat,dog\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_alpha(sort_alpha)" + "test_sort(sort_alpha)" ] }, { @@ -376,23 +661,55 @@ "outputs": [], "source": [ "def check_pass(password):\n", - "#your code here" + " \"\"\"Check if the password meets the criteria.\"\"\"\n", + " if len(password) < 8:\n", + " return False\n", + " \n", + " has_upper = False\n", + " has_lower = False\n", + " has_digit = False\n", + " has_special = False\n", + " special_characters = \"!@#$%^&*()-+\"\n", + " \n", + " for char in password:\n", + " if char.isupper():\n", + " has_upper = True\n", + " elif char.islower():\n", + " has_lower = True\n", + " elif char.isdigit():\n", + " has_digit = True\n", + " elif char in special_characters:\n", + " has_special = True\n", + " \n", + " return has_upper and has_lower and has_digit and has_special \n", + "def test_password(func):\n", + " print(check_pass(\"Ammulu@9989\")) # Output: True\n", + " print(check_pass(\"pass\")) # Output: False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 135, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_pass(check_pass)" + "test_password(check_pass)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -406,12 +723,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.7" } }, "nbformat": 4, diff --git a/mod/__pycache__/testing.cpython-313.pyc b/mod/__pycache__/testing.cpython-313.pyc new file mode 100644 index 0000000..2220b4c Binary files /dev/null and b/mod/__pycache__/testing.cpython-313.pyc differ