From 6749c272ac00ded95cc6b1038f9b65ba8eb69338 Mon Sep 17 00:00:00 2001 From: Patricia Zapata Blanco Date: Thu, 12 Oct 2023 23:39:46 +0200 Subject: [PATCH] [Pati] completed lab-functions --- main.ipynb | 475 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 416 insertions(+), 59 deletions(-) diff --git a/main.ipynb b/main.ipynb index 2b94ca7..aa65a14 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -37,19 +37,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - " pass" + " if a > b:\n", + " greater_num = a\n", + " elif a < b:\n", + " greater_num = b\n", + " else:\n", + " print(f\"Numbers are equal\")\n", + " return greater_num" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.115s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" @@ -64,19 +82,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def greatest(arr):\n", - " pass" + " arr.sort()\n", + " largest = arr[-1]\n", + " return largest" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.129s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greatest(greatest)" @@ -91,19 +123,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def sum_all(arr):\n", - " pass" + " counter = 0\n", + " for i in arr:\n", + " counter += i\n", + " return counter" ] }, { "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.126s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -118,19 +165,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def mult_all(arr):\n", - " pass" + " multiplication = 1\n", + " for i in arr:\n", + " multiplication*=i\n", + " return multiplication" ] }, { "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.113s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -145,19 +207,69 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 78, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "def oper_all(arr, oper):\n", - " pass" + " \n", + " suma = 0\n", + " mult = 1\n", + " \n", + " if oper == \"+\":\n", + " for i in arr:\n", + " suma += i\n", + " return suma\n", + "\n", + " if oper == \"*\":\n", + " for i in arr:\n", + " mult *= i\n", + " return mult\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 80, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "array = [1,2,3]\n", + "oper_all(array,\"*\")" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.113s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -172,19 +284,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", - " pass" + " factorial = 1\n", + " while n > 0:\n", + " factorial*=n\n", + " n-=1\n", + " return factorial" ] }, { "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.095s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -201,19 +329,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 230, "metadata": {}, "outputs": [], "source": [ "def unique(arr):\n", - " pass" + " unique_list = []\n", + " for i in arr:\n", + " if i not in unique_list:\n", + " unique_list+= [i]\n", + " return unique_list" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 231, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 5, 6]" + ] + }, + "execution_count": 231, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arra = [1,2,3,5,6,6,6]\n", + "unique(arra)" + ] + }, + { + "cell_type": "code", + "execution_count": 232, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.106s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -229,12 +394,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 175, "metadata": {}, "outputs": [], "source": [ "def st_dev(arr):\n", - " pass" + " suma = 0\n", + " length = 0\n", + " deviation = 0\n", + " \n", + " for i in arr:\n", + " suma += i\n", + "\n", + " for i in arr:\n", + " length +=1\n", + " \n", + " mean = suma / length\n", + "\n", + " for i in arr:\n", + " deviation += ((i-mean)**2)\n", + " var = deviation / (length -1)\n", + " st_dev = var**0.5\n", + " return st_dev\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 176, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.160246899469287" + ] + }, + "execution_count": 176, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arrays = [1,2,3,4,5,6,7]\n", + "st_dev(arrays)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 173, + "metadata": {}, + "outputs": [], + "source": [ + "import statistics as stats" + ] + }, + { + "cell_type": "code", + "execution_count": 174, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.160246899469287" + ] + }, + "execution_count": 174, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stats.stdev(arrays)" ] }, { @@ -261,19 +495,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 303, "metadata": {}, "outputs": [], "source": [ "def pangram(string):\n", - " pass" + " pangram = True\n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + " for i in alphabet:\n", + " if i not in string.lower():\n", + " pangram = False\n", + " break\n", + " return pangram\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 304, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.028s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -289,19 +541,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 275, "metadata": {}, "outputs": [], "source": [ "def check_pass(string):\n", - " pass" + " special_characters = \"#@!$%&()^*[]{}\"\n", + " length = 0\n", + " length_more_than_8 = False\n", + " lower_case = False\n", + " upper_case = False\n", + " number = False\n", + " special_char = False\n", + " \n", + " for i in string:\n", + " length +=1\n", + " if length >= 8:\n", + " length_more_than_8 = True\n", + " \n", + " for i in string:\n", + " if i.islower():\n", + " lower_case = True\n", + " break\n", + " \n", + " for i in string:\n", + " if i.isupper():\n", + " upper_case = True\n", + " break\n", + " \n", + " for i in string:\n", + " if i.isdigit():\n", + " number = True\n", + " break\n", + " \n", + " for i in string:\n", + " if i in special_characters:\n", + " special_char = True\n", + " break\n", + " \n", + " if length_more_than_8 and lower_case and upper_case and number and special_char:\n", + " return True\n", + " \n", + " return False" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 276, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.108s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" @@ -324,19 +624,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 309, "metadata": {}, "outputs": [], "source": [ "def mode_counter(arr):\n", - " pass" + " frequency={}\n", + " for i in arr:\n", + " if i in frequency.keys():\n", + " frequency[i]+=1\n", + " else:\n", + " frequency[i]=1\n", + " max_freq=0\n", + " for a,b in frequency.items():\n", + " if b>max_freq:\n", + " mode=a\n", + " max_freq=b\n", + " return mode\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 310, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.063s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -353,23 +678,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 327, "metadata": {}, "outputs": [], "source": [ "def sort_alpha(string):\n", - " pass" + " lst=[]\n", + " word=''\n", + " for i in string:\n", + " if i!= ',':\n", + " word+= i\n", + " else:\n", + " lst+= [word]\n", + " word=''\n", + " lst+=[word]\n", + " lst.sort()\n", + " new_string=''\n", + " for w in lst:\n", + " new_string+=w+','\n", + " return new_string[:-1]" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 328, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.053s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_alpha(sort_alpha)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -388,7 +745,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.4" }, "toc": { "base_numbering": 1,