diff --git a/main.ipynb b/main.ipynb index b05630a..26ccd07 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,12 +20,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from mod.testing import *\n", - "import unittest" + "import unittest\n" ] }, { @@ -41,18 +41,23 @@ "metadata": {}, "outputs": [], "source": [ - "def greater(a,b):\n", - "#your code here" + "def greater(a, b):\n", + " test_greater(greater)\n", + "#your code here " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ - "# This will test your function \n", - "test_greater(greater)" + "def find_greater(a: 5, b: 10) -> int:\n", + " if a > b:\n", + " return a\n", + " else:\n", + " return b\n", + " print(find_greater(5, 10))" ] }, { @@ -76,18 +81,38 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "#your code here" - ] + "source": [] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# This will test your function \n", - "test_greatest(greatest)" + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "x = [3, 10, 5, 7, 4]\n", + "\n", + "def find_largest(numbers: list) -> int:\n", + " if numbers == []:\n", + " return None\n", + "\n", + " \n", + " largest = numbers[0]\n", + "\n", + " \n", + " for num in numbers:\n", + " if num > largest:\n", + " largest = num\n", + "\n", + " return largest\n", + "print( find_largest(x))\n" ] }, { @@ -99,12 +124,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ - "def sum_all(lst):\n", - "#your code here" + "def sum_all(lst):[5,2,3,4]\n" ] }, { @@ -113,10 +137,30 @@ "metadata": { "scrolled": true }, - "outputs": [], - "source": [ - "# This will test your function \n", - "test_sum(sum_all)" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "def sum_all_while(lst):\n", + " total = 0\n", + " i = 0\n", + " while i < len(lst): \n", + " total = total + lst[i]\n", + " i = i + 1\n", + " return total\n", + "\n", + "# Test\n", + "print(sum_all_while([5, 2, 3, 4]))\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -128,24 +172,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ - "def mult_all(lst):\n", - "#your code here" + "def mult_all(lst):[5,2,3,4]\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120\n" + ] + } + ], "source": [ - "# This will test your function \n", - "test_mult(mult_all)" + "\n", + "def multiply_all_while(lst):\n", + " product = 1\n", + " i = 0\n", + " while i < len(lst):\n", + " product = product * lst[i]\n", + " i += 1\n", + " return product\n", + "\n", + "print(multiply_all_while([5,2,3,4]))\n" ] }, { @@ -157,12 +216,46 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120\n" + ] + } + ], + "source": [ + "def oper_all(arr, oper = \"*\"):[5, 2, 3, 4]\n", + "def oper_all(arr, oper=\"*\"):\n", + " \"\"\"\n", + " Receives a list of numbers and an operator ('+' or '*').\n", + " Returns the result of applying the operation on all elements.\n", + " \"\"\"\n", + " \n", + " if arr == []:\n", + " # Return 0 for sum, 1 for multiplication if list is empty\n", + " return 0 if oper == \"+\" else 1\n", + " \n", + " if oper == \"+\":\n", + " result = 0\n", + " for num in arr:\n", + " result += num\n", + " return result\n", + " elif oper == \"*\":\n", + " result = 1\n", + " for num in arr:\n", + " result *= num\n", + " return result\n", + " else:\n", + " return None # Invalid operator\n", + "\n", + "\n", + "# 🧪 Example tests\n", + "print(oper_all([5, 2, 3, 4]))\n", + "\n" ] }, { @@ -171,8 +264,8 @@ "metadata": {}, "outputs": [], "source": [ - "# This will test your function \n", - "test_operations(oper_all)" + "\n", + "test_operations(oper_all)\n" ] }, { @@ -184,12 +277,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ - "def factorial(n):\n", - "#your code here" + "def factorial(n):5\n" ] }, { @@ -213,12 +305,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# This will test your function \n", - "test_factorial(factorial)" + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120\n" + ] + } + ], + "source": [ + " \n", + "def factorial(n):\n", + " if n < 0:\n", + " return None \n", + "\n", + " result = 1\n", + " for i in range(1, n + 1):\n", + " result = result * i \n", + "\n", + " return result \n", + "\n", + "\n", + "\n", + "print(factorial(5)) \n", + "\n", + "\n" ] }, { @@ -232,22 +346,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": {}, "outputs": [], "source": [ - "def unique(lst_un):\n", - "#your code here" + "def unique(lst_un):[10, 20, 30, 20, 10 , 40, 50 , 30]\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30, 40, 50]\n" + ] + } + ], "source": [ - "# This will test your function \n", - "test_unique(unique)" + "\n", + "def unique(lst_un):\n", + " \n", + " unique_lst = [] \n", + " \n", + " for item in lst_un:\n", + " if item not in unique_lst: \n", + " unique_lst.append(item)\n", + " \n", + " return unique_lst\n", + "\n", + "numbers = [10, 20, 30, 20, 10, 40, 50, 30]\n", + "print(unique(numbers)) \n" ] }, { @@ -260,12 +392,51 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "def mode_counter(arr):\n", - "#your code here" + " \"\"\"Return the mode (most frequent element) of a list.\"\"\"\n", + " if arr == []:\n", + " return None # empty list has no mode\n", + " \n", + " frequency = [] # list to store [element, count]\n", + " \n", + " # Count occurrences manually\n", + " for item in arr:\n", + " found = False\n", + " for pair in frequency:\n", + " if pair[0] == item:\n", + " pair[1] += 1 # increment count\n", + " found = True\n", + " break\n", + " if not found:\n", + " frequency.append([item, 1]) # add new element with count 1\n", + " \n", + " # Find the element with the highest count\n", + " max_count = 0\n", + " mode_value = None\n", + " for pair in frequency:\n", + " if pair[1] > max_count:\n", + " max_count = pair[1]\n", + " mode_value = pair[0]\n", + " \n", + " return mode_value\n", + "\n", + "\n", + "# Example usage\n", + "numbers = [1, 2, 3, 4, 2, 2, 1, 1, 4, 5, 1]\n", + "print(mode_counter(numbers)) # Output: 1\n", + "\n" ] }, { @@ -273,10 +444,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# This will test your function \n", - "test_mode(mode_counter)" - ] + "source": [] }, { "cell_type": "markdown", @@ -292,18 +460,43 @@ "metadata": {}, "outputs": [], "source": [ - "def st_dev(list_sd):\n", - "#your code here" + "def st_dev(list_sd):\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# This will test your function \n", - "test_stdev(st_dev)" + "\n", + "def st_dev(list_sd):\n", + " n = 0\n", + " for _ in list_sd:\n", + " n = n + 1\n", + " total = 0\n", + " for x in list_sd:\n", + " total = total + x\n", + " mean = total / n\n", + " variance_sum = 0\n", + " for x in list_sd:\n", + " diff = x - mean\n", + " variance_sum = variance_sum + diff * diff\n", + " if n > 1:\n", + " variance = variance_sum / (n - 1)\n", + " else:\n", + " variance = 0 \n", + " std_dev = variance ** 0.5\n", + " return std_dev \n", + "print()" ] }, { @@ -317,10 +510,37 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n" + ] + } + ], "source": [ "def pangram(string):\n", - "#your code here" + " def pangram(string):\n", + " \"\"\"Return True if string is a pangram, False otherwise.\"\"\"\n", + " \n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + " string = string.lower() \n", + " \n", + " for letter in alphabet:\n", + " if letter not in string:\n", + " return False \n", + " \n", + " return True \n", + "\n", + "\n", + "# 🧪 Example tests\n", + "print(pangram(\"The quick brown fox jumps over the lazy dog\")) # True\n", + "print(pangram(\"Hello World\")) # False\n", + "print(pangram(\"Pack my box with five dozen liquor jugs.\")) # True\n", + "\n" ] }, { @@ -344,12 +564,46 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple,banana,cherry\n", + "ant,bird,cat,dog\n", + "ant,elephant,Lion,Zebra\n", + "\n" + ] + } + ], "source": [ + "\n", "def sort_alpha(string):\n", - "#your code here" + " \"\"\"Return comma-separated words sorted alphabetically (bubble sort).\"\"\"\n", + " \n", + " if string == \"\":\n", + " return \"\"\n", + " \n", + " # Split and strip spaces\n", + " words = [word.strip() for word in string.split(\",\")]\n", + " \n", + " # Bubble sort\n", + " n = len(words)\n", + " for i in range(n):\n", + " for j in range(0, n-i-1):\n", + " if words[j].lower() > words[j+1].lower():\n", + " words[j], words[j+1] = words[j+1], words[j]\n", + " \n", + " return \",\".join(words)\n", + "\n", + "\n", + "# 🧪 Example tests\n", + "print(sort_alpha(\"banana, apple, cherry\")) \n", + "print(sort_alpha(\"dog,cat,ant,bird\")) \n", + "print(sort_alpha(\"Zebra, elephant, Lion, ant\"))\n", + "print(sort_alpha(\"\")) \n" ] }, { @@ -357,10 +611,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# This will test your function \n", - "test_alpha(sort_alpha)" - ] + "source": [] }, { "cell_type": "markdown", @@ -373,10 +624,47 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], "source": [ "def check_pass(password):\n", - "#your code here" + " \"\"\"Return True if password is strong, False otherwise.\"\"\"\n", + " \n", + " if len(password) < 8:\n", + " return False # too short\n", + " \n", + " has_lower = False\n", + " has_upper = False\n", + " has_digit = False\n", + " has_special = False\n", + " \n", + " special_chars = \"!@#$%^&*()-_+=<>?/.,\"\n", + " \n", + " for char in password:\n", + " if char.islower():\n", + " has_lower = True\n", + " elif char.isupper():\n", + " has_upper = True\n", + " elif char.isdigit():\n", + " has_digit = True\n", + " elif char in special_chars:\n", + " has_special = True\n", + " \n", + " return has_lower and has_upper and has_digit and has_special\n", + "\n", + "\n", + "# Test the function\n", + "print(check_pass(\"Abc123!@\")) \n", + "print(check_pass(\"abc123\")) \n", + "\n" ] }, { @@ -384,15 +672,12 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# This will test your function \n", - "test_pass(check_pass)" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -406,12 +691,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.5" } }, "nbformat": 4, diff --git a/mod/__pycache__/testing.cpython-313.pyc b/mod/__pycache__/testing.cpython-313.pyc new file mode 100644 index 0000000..cfaa10a Binary files /dev/null and b/mod/__pycache__/testing.cpython-313.pyc differ