diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..4eccaf6 Binary files /dev/null and b/.DS_Store differ diff --git a/main.ipynb b/main.ipynb index b05630a..30ef29c 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -37,57 +37,103 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - "#your code here" + " if a > b:\n", + " return a\n", + " else:\n", + " return b\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.563s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "#your code here" + "## 2. Now write a function that returns the largest element on a list" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 6, "metadata": {}, + "outputs": [], "source": [ - "## 2. Now write a function that returns the largest element on a list" + "def largest_num(lst):\n", + " largest = lst[0]\n", + " for num in lst:\n", + " if num > largest:\n", + " largest = num \n", + "\n", + " return largest\n", + "\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "def largest_in_list(numbers):\n", + " # assume the first element is the largest to start\n", + " largest = numbers[0]\n", + "\n", + " # loop through the list and update largest if we find a bigger number\n", + " for num in numbers:\n", + " if num > largest:\n", + " largest = num\n", + "\n", + " return largest" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.396s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_greatest(greatest)" + "test_greatest(largest_in_list)" ] }, { @@ -99,21 +145,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "def sum_all(lst):\n", - "#your code here" + "def sum_all(numbers):\n", + " total = 0\n", + " for num in numbers:\n", + " total += num\n", + "\n", + " return total\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.582s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -128,21 +190,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def mult_all(lst):\n", - "#your code here" + " product = 1 # start from 1 because multiplying by 0 would always give 0\n", + " \n", + " for num in lst:\n", + " product *= num\n", + " return product" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.369s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -157,19 +235,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "\n", + " if oper == \"+\":\n", + " total = 0\n", + " for num in arr:\n", + " total += num\n", + " return total\n", + " elif oper == \"*\":\n", + " product = 1\n", + " for num in arr:\n", + " product *= num\n", + " return product\n", + " else:\n", + " return \"Invalid operator\"\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.576s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -184,17 +286,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", - "#your code here" + " \n", + " if n < 0:\n", + " return \"Factorial not defined for negative numbers\"\n", + " \n", + " result = 1\n", + " for i in range(1, n + 1):\n", + " result *= i\n", + " return result" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -213,9 +322,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.530s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -232,19 +353,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def unique(lst_un):\n", - "#your code here" + " unique_list = []\n", + " for item in lst_un:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.687s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -260,19 +397,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def mode_counter(arr):\n", - "#your code here" + " freq = {}\n", + "\n", + " for item in arr:\n", + " if item in freq:\n", + " freq[item] += 1\n", + " else:\n", + " freq[item] = 1\n", + "\n", + " max_count = 0\n", + " mode_value = None\n", + "\n", + " for key in freq:\n", + " if freq[key] > max_count:\n", + " max_count = freq[key]\n", + " mode_value = key\n", + "\n", + " return mode_value\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.498s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -288,19 +454,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def st_dev(list_sd):\n", - "#your code here" + " \n", + " # 1 Calculate the length manually\n", + " n = 0\n", + " for _ in list_sd:\n", + " n += 1\n", + "\n", + " # 2 Calculate the mean manually\n", + " total = 0\n", + " for x in list_sd:\n", + " total += x\n", + " mean = total / n\n", + "\n", + " # 3 Calculate the sum of squared differences\n", + " squared_diff_sum = 0\n", + " for x in list_sd:\n", + " squared_diff_sum += (x - mean) ** 2\n", + "\n", + " # 4 Apply Bessel's correction (divide by n - 1)\n", + " variance = squared_diff_sum / (n - 1)\n", + "\n", + " # 5 Standard deviation is the square root of variance\n", + " std_dev = variance ** 0.5\n", + " return std_dev" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.500s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_stdev(st_dev)" @@ -315,19 +515,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def pangram(string):\n", - "#your code here" + "\n", + " # 1 Convert letters to lowercase, to check case sensitive\n", + " string = string.lower()\n", + "\n", + " # 2 Keep track of letters we've seen\n", + " letters_seen = []\n", + "\n", + " # 3 Go through each character and store it if it's a letter a-z\n", + " for ch in string:\n", + " if \"a\" <= ch <= \"z\" and ch not in letters_seen:\n", + " letters_seen.append(ch)\n", + "\n", + " # 4 Count letters \n", + " count = 0\n", + " for _ in letters_seen:\n", + " count += 1\n", + "\n", + " # 5 Check if we saw all 26 letters\n", + " if count == 26:\n", + " return True\n", + " else:\n", + " return False " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "." + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.128s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -344,19 +584,71 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def sort_alpha(string):\n", - "#your code here" + "\n", + " # 1) Manually split by commas (no split)\n", + " words = []\n", + " current = \"\"\n", + " for ch in string:\n", + " if ch == \",\":\n", + " words += [current] # avoiding append()\n", + " current = \"\"\n", + " else:\n", + " current += ch\n", + " words += [current] # add the last token\n", + "\n", + " # 2) Compute length manually (no len)\n", + " n = 0\n", + " for _ in words:\n", + " n += 1\n", + "\n", + " # 3) Bubble sort (no sorted)\n", + " i = 0\n", + " while i < n:\n", + " j = 0\n", + " limit = n - i - 1\n", + " while j < limit:\n", + " if words[j] > words[j + 1]:\n", + " tmp = words[j]\n", + " words[j] = words[j + 1]\n", + " words[j + 1] = tmp\n", + " j += 1\n", + " i += 1\n", + "\n", + " # 4) Rebuild the CSV string (no join)\n", + " result = \"\"\n", + " idx = 0\n", + " last = n - 1\n", + " while idx < n:\n", + " result += words[idx]\n", + " if idx < last:\n", + " result += \",\"\n", + " idx += 1\n", + "\n", + " return result\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.588s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_alpha(sort_alpha)" @@ -371,19 +663,58 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "False\n", + "False\n" + ] + } + ], "source": [ "def check_pass(password):\n", - "#your code here" + " if len(password) < 8:\n", + " return False\n", + "\n", + " has_lower = any(c.islower() for c in password)\n", + " has_upper = any(c.isupper() for c in password)\n", + " has_digit = any(c.isdigit() for c in password)\n", + " has_special = any(not c.isalnum() for c in password) # not letter or number\n", + "\n", + " return all([has_lower, has_upper, has_digit, has_special])\n", + "\n", + "\n", + "# Examples\n", + "print(check_pass(\"StrongPass1!\")) # ✅ True\n", + "print(check_pass(\"weakpass\")) # ❌ False\n", + "print(check_pass(\"HELLO123\")) # ❌ False\n", + "print(check_pass(\"Hi!2\")) # ❌ False\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.400s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" @@ -392,7 +723,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -406,12 +737,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..b01c0c4 Binary files /dev/null and b/mod/__pycache__/testing.cpython-313.pyc differ