From 1a43e3f61cb1c4ee39cd4a33fdd5142e17683c17 Mon Sep 17 00:00:00 2001 From: "DESKTOP-C5KON3H\\Javier" Date: Thu, 9 Oct 2025 20:12:05 +0200 Subject: [PATCH] functions --- main.ipynb | 565 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 493 insertions(+), 72 deletions(-) diff --git a/main.ipynb b/main.ipynb index b05630a..21ecd42 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -37,19 +37,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - "#your code here" + "#your code here\n", + " return max(a,b)" ] }, { "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.088s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" @@ -57,11 +70,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "2\n", + "2\n" + ] + } + ], "source": [ - "#your code here" + "#your code here\n", + "print(greater(1,2))\n", + "print(greater(2,1))\n", + "print(greater(2,2))" ] }, { @@ -73,18 +99,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "#your code here\n", + "import sys\n", + "\n", + "def greatest(list):\n", + " min_int = -sys.maxsize - 1\n", + " for i in list:\n", + " if i > min_int:\n", + " min_int = i\n", + "\n", + " return min_int\n", + "\n" ] }, { "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.131s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greatest(greatest)" @@ -99,21 +147,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def sum_all(lst):\n", - "#your code here" + "#your code here\n", + " sum = 0\n", + " for i in lst:\n", + " sum += i\n", + " return sum" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.099s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -128,21 +192,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def mult_all(lst):\n", - "#your code here" + "#your code here\n", + " prod = 1\n", + " for i in lst:\n", + " prod *= i\n", + " return prod" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.098s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -157,19 +237,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "#your code here\n", + " if oper == \"*\":\n", + " prod = 1\n", + " for i in arr:\n", + " prod *= i\n", + " return prod\n", + " elif oper == \"+\":\n", + " suma = 0\n", + " for i in arr:\n", + " suma += i\n", + " return suma\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.087s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -184,17 +286,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", - "#your code here" + "#your code here\n", + " fact = 1\n", + " while n > 0:\n", + " fact *= n\n", + " n -= 1\n", + " return fact\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -213,9 +320,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.090s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -232,19 +351,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def unique(lst_un):\n", - "#your code here" + "#your code here\n", + " empty_list = []\n", + " for i in lst_un:\n", + " if i not in empty_list:\n", + " empty_list.append(i)\n", + " return empty_list" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.183s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -260,19 +396,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "def mode_counter(arr):\n", - "#your code here" + "#your code here\n", + " max_count = 0;\n", + " mod = 'asdf'\n", + " for i in arr:\n", + " count = 0\n", + " for j in arr:\n", + " if j == i:\n", + " count +=1\n", + " if count > max_count:\n", + " mod = i\n", + " max_count = count\n", + " return mod\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.215s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -288,19 +449,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ + "import math\n", + "\n", "def st_dev(list_sd):\n", - "#your code here" + "#your code here\n", + " mean = sum(list_sd) / len(list_sd)\n", + " deviation_each = sum((i-mean)** 2 for i in list_sd)\n", + " avg = deviation_each / (len(list_sd) - 1)\n", + " return avg ** 0.5" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.143s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_stdev(st_dev)" @@ -315,19 +494,192 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "def pangram(string):\n", - "#your code here" + "#your code here\n", + " letters = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"n\",\"m\",\"ñ\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\",\n", + " \"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"0\"]\n", + " for i in letters:\n", + " if i.lower() not in string:\n", + " return False\n", + " return True\n", + "\n", + "print(pangram(\"asdfghjklñqwertyuiozxcvvbnmñ\"))\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "FFFFFFFFFFFFFFFF....F...F.....\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "======================================================================\n", + "FAIL: runTest (mod.testing.test_pangram..TestKnown.runTest)\n", + "----------------------------------------------------------------------\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Javier\\iron-hack\\week2\\lab1\\mod\\testing.py\", line 172, in runTest\n", + " self.assertEqual(fn(self.input), self.output, f\"Should be {self.output}\")\n", + "AssertionError: False != True : Should be True\n", + "\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.035s\n", + "\n", + "FAILED (failures=18)\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -344,19 +696,50 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'asdfa,ddasd,fdsfds.asdf'" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def sort_alpha(string):\n", - "#your code here" + "#your code here\n", + " values = string.split(\",\")\n", + " values.sort()\n", + " new_string = ''\n", + " for i in values:\n", + " new_string += i + ','\n", + " return new_string.rstrip(',')\n", + "\n", + "sort_alpha(\"ddasd,asdfa,fdsfds.asdf\")" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.096s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_alpha(sort_alpha)" @@ -371,30 +754,68 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 128, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "def check_pass(password):\n", - "#your code here" + "#your code here\n", + " length = len(password) >= 8\n", + " lowercase = any(i.islower() for i in password)\n", + " uppercase = any(i.isupper() for i in password)\n", + " digit = any(i.isdigit() for i in password)\n", + " special_char = any(not i.isalnum() for i in password)\n", + " if length and uppercase and lowercase and digit and special_char:\n", + " return True\n", + " return False\n", + " \n", + "\n", + "print(check_pass(\"asdfaTTdfA1.\"))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 129, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.067s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -406,7 +827,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" }, "vscode": { "interpreter": {