From 20342ba0f6b4a80ddefaa75a06c7fd3572ce7f78 Mon Sep 17 00:00:00 2001 From: nimsilvestre Date: Wed, 8 Oct 2025 20:03:06 +0100 Subject: [PATCH] Add completed file --- main.ipynb | 478 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 396 insertions(+), 82 deletions(-) diff --git a/main.ipynb b/main.ipynb index b05630a..c3fa223 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -37,33 +37,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - "#your code here" + "#your code here\n", + " if a > b:\n", + " return a\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.081s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -77,14 +83,33 @@ "metadata": {}, "outputs": [], "source": [ - "#your code here" + "def greatest(lst):\n", + "#your code here\n", + " max_value = lst[0]\n", + " for value in lst:\n", + " if value > max_value:\n", + " max_value = value\n", + "\n", + " return max_value" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.083s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greatest(greatest)" @@ -104,16 +129,32 @@ "outputs": [], "source": [ "def sum_all(lst):\n", - "#your code here" + "#your code here\n", + " total = 0\n", + " for num in lst:\n", + " total += num\n", + " return total" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.082s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -133,16 +174,32 @@ "outputs": [], "source": [ "def mult_all(lst):\n", - "#your code here" + "#your code here\n", + " total = 1 #multiplying by 0 it's always 0\n", + " for num in lst:\n", + " total *= num\n", + " return total" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.079s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -157,19 +214,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def oper_all(arr, oper=\"+\"):\n", + " if oper == \"+\":\n", + " result = 0\n", + " for num in arr:\n", + " result += num\n", + " else:\n", + " result = 1\n", + " for num in arr:\n", + " result *= num\n", + " return result\n", + "\n", + "arr = [2,5,10]\n", + "oper_all(arr, oper = '+')" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.078s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -184,17 +275,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5040" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def factorial(n):\n", - "#your code here" + "#your code here\n", + " result = 1\n", + " i = 1\n", + " while i <= n:\n", + " result *= i\n", + " i += 1\n", + " return result\n", + "\n", + "factorial(7)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, "outputs": [], "source": [ @@ -213,9 +323,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.086s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -232,19 +354,58 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def unique(lst_un):\n", - "#your code here" + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 5, 1, 8, 4, 23]" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def unique(lst):\n", + "#your code here\n", + " unique_list = []\n", + " for item in lst:\n", + " # verify if item is in lista unique_list\n", + " found = False\n", + " for x in unique_list:\n", + " if x == item:\n", + " found = True\n", + " break\n", + " # if not, add it using concatenation\n", + " if not found:\n", + " unique_list = unique_list + [item]\n", + " return unique_list\n", + "\n", + "\n", + "lista_unica = [3,5,1,1,8,4,23,4,1]\n", + "unique(lista_unica)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.270s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -265,14 +426,39 @@ "outputs": [], "source": [ "def mode_counter(arr):\n", - "#your code here" + "#your code here\n", + " max_count = 0\n", + " mode = arr[0]\n", + " for i in arr:\n", + " count = 0\n", + " # counts how many times i appear\n", + " for j in arr:\n", + " if i == j:\n", + " count += 1\n", + " # update the mode occouring number is higher\n", + " if count > max_count:\n", + " max_count = count\n", + " mode = i\n", + " return mode" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.206s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -292,15 +478,53 @@ "metadata": {}, "outputs": [], "source": [ - "def st_dev(list_sd):\n", - "#your code here" + "def st_dev(lst):\n", + "#your code here\n", + " total = 0\n", + " count = 0\n", + " for num in lst:\n", + " total += num\n", + " count += 1\n", + " mean = total / count\n", + "\n", + " sq_diff_sum = 0\n", + " for num in lst:\n", + " diff = num - mean\n", + " sq_diff_sum += diff * diff\n", + "\n", + " # Use sample variance (divide by count - 1).\n", + " variance = sq_diff_sum / (count - 1) if count > 1 else 0\n", + "\n", + " if variance == 0:\n", + " return 0\n", + "\n", + " guess = variance / 2 if variance >= 1 else 1\n", + " for _ in range(100):\n", + " new_guess = (guess + variance / guess) / 2\n", + " if abs(new_guess - guess) < 1e-10:\n", + " break\n", + " guess = new_guess\n", + "\n", + " return guess" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.082s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_stdev(st_dev)" @@ -315,19 +539,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "def pangram(string):\n", - "#your code here" + "#your code here\n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + "\n", + " for letter in alphabet:\n", + " found = False\n", + " for char in string.lower():\n", + " if char == letter:\n", + " found = True\n", + " break\n", + " if not found:\n", + " return False\n", + " return True" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.027s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -344,19 +591,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "def sort_alpha(string):\n", - "#your code here" + "#your code here\n", + " words = []\n", + " current_word = \"\"\n", + " # Loop through each character in the string\n", + " for c in string:\n", + " # When we find a comma, add the current word to the list\n", + " if c == \",\":\n", + " words = words + [current_word.strip()]\n", + " current_word = \"\"\n", + " else:\n", + " # Otherwise, keep adding characters to the current word\n", + " current_word += c\n", + " # Add the last word after the loop ends\n", + " words = words + [current_word.strip()]\n", + "\n", + " # Sort the list of words alphabetically\n", + " sorted_words = sorted(words)\n", + "\n", + " # Build the sorted string without using join()\n", + " result = \"\"\n", + " for i in range(len(sorted_words)):\n", + " result += sorted_words[i]\n", + " # Add comma between words except after the last word\n", + " if i != len(sorted_words) - 1:\n", + " result += \",\"\n", + " return result\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.154s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_alpha(sort_alpha)" @@ -371,19 +655,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "def check_pass(password):\n", - "#your code here" + "#your code here\n", + "\n", + " # Check minimum length\n", + " if len(password) < 8:\n", + " return False\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 \"a\" <= char <= \"z\":\n", + " has_lower = True\n", + " elif \"A\" <= char <= \"Z\":\n", + " has_upper = True\n", + " elif \"0\" <= char <= \"9\":\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" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.080s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" @@ -392,7 +711,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -406,12 +725,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.9.6" } }, "nbformat": 4,