From 8e5f3e3f09e254b9147af0220ab8aac620a53b6e Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 12 Oct 2023 20:26:50 +0200 Subject: [PATCH 1/2] Lab functions done --- main.ipynb | 352 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 301 insertions(+), 51 deletions(-) diff --git a/main.ipynb b/main.ipynb index 2b94ca7..2e1d93d 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -37,19 +37,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 222, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - " pass" + " if a > b:\n", + " return a\n", + " else:\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 223, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.032s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" @@ -64,19 +79,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 260, "metadata": {}, "outputs": [], "source": [ "def greatest(arr):\n", - " pass" + " \n", + " largest = 0\n", + " \n", + " for number in arr:\n", + " if number > largest:\n", + " largest = number\n", + "\n", + " return largest" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 262, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.033s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greatest(greatest)" @@ -91,19 +125,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 267, "metadata": {}, "outputs": [], "source": [ "def sum_all(arr):\n", - " pass" + " \n", + " total_sum = 0\n", + " \n", + " for nums in arr:\n", + " total_sum += nums\n", + " return total_sum\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 269, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.041s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -118,19 +169,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 273, "metadata": {}, "outputs": [], "source": [ "def mult_all(arr):\n", - " pass" + " \n", + " mult_total = 1\n", + " \n", + " for nums in arr:\n", + " mult_total *= nums\n", + " return mult_total" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 274, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.038s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -145,19 +213,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 320, "metadata": {}, "outputs": [], "source": [ "def oper_all(arr, oper):\n", - " pass" + " \n", + " arr_11 = 1\n", + " total_sum = 0\n", + " \n", + " if oper == \"+\":\n", + " for nums in arr:\n", + " total_sum += nums\n", + " return total_sum\n", + "\n", + " if oper == \"*\":\n", + " for nums in arr:\n", + " arr_11 *= nums\n", + " arr = arr_11\n", + " \n", + " return arr" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 322, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.033s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -182,9 +276,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 350, "metadata": {}, "outputs": [], + "source": [ + "def factorial(n):\n", + " \n", + " factorial = 1\n", + " \n", + " if n < 0:\n", + " pass\n", + " \n", + " while n > 0:\n", + " factorial *= n\n", + " n -= 1\n", + " return factorial" + ] + }, + { + "cell_type": "code", + "execution_count": 349, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.035s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -201,19 +326,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 351, "metadata": {}, "outputs": [], "source": [ "def unique(arr):\n", - " pass" + " \n", + " uniq_values = []\n", + " \n", + " for nums in arr:\n", + " if nums not in uniq_values:\n", + " uniq_values += [nums]\n", + " \n", + " return uniq_values\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 352, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.089s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -229,22 +375,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 356, "metadata": {}, "outputs": [], "source": [ "def st_dev(arr):\n", - " pass" + "\n", + " n = 0\n", + " sum_data = 0\n", + " sum_squared_diff = 0\n", + " \n", + " for x in arr:\n", + " n+= 1\n", + " sum_data += x\n", + " \n", + " mean = sum_data / n\n", + " \n", + " for x in arr:\n", + " squared_diff = (x - mean) ** 2\n", + " sum_squared_diff += squared_diff\n", + " \n", + " if n < 2:\n", + " return None\n", + " \n", + " variance = sum_squared_diff / (n - 1)\n", + " std_dev = variance ** 0.5\n", + " \n", + " return std_dev\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 357, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.034s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function\n", - "#test_stdev(st_dev)\n", + "test_stdev(st_dev)\n", "\n", "# Our tests need to be improved, so for this exercise, we care about the code itself. You may check if you get\n", "# similar results by checking with:\n", @@ -261,19 +441,57 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def pangram(string):\n", - " pass" + " letter_dict = {}\n", + " \n", + " count = 0\n", + "\n", + " for char in string.lower():\n", + " if 'a' <= char <= 'z':\n", + " if char not in letter_dict:\n", + " letter_dict[char] = True\n", + " count += 1\n", + " return count == 26\n", + "\n", + "\n", + "sentence = \"The quick brown fox jumps over the lazy dog\"\n", + "pangram(sentence)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.033s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -289,19 +507,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": {}, "outputs": [], "source": [ "def check_pass(string):\n", - " pass" + " minuscule = False\n", + " numbers = False\n", + " capital_letter = False\n", + " special_chars_pattern = r\"[!@#$%^&*()_+{}\\[\\]:;<>,.?~\\\\]\"\n", + " special_chars = False\n", + " \n", + " if len(string) < 8:\n", + " return False\n", + " \n", + " for char in string:\n", + " if char.islower():\n", + " minuscule = True\n", + " if char.isupper():\n", + " capital_letter = True\n", + " if char.isdigit():\n", + " numbers = True\n", + " if char in special_chars_pattern:\n", + " special_chars = True\n", + "\n", + " return minuscule and capital_letter and numbers and special_chars\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.103s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" @@ -388,7 +638,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.3" }, "toc": { "base_numbering": 1, From 7a27889483ca388979356a30e466e85d260c2165 Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 13 Oct 2023 09:52:28 +0200 Subject: [PATCH 2/2] Uploaded --- main.ipynb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.ipynb b/main.ipynb index 2e1d93d..7edc497 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -507,7 +507,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -531,13 +531,16 @@ " if char in special_chars_pattern:\n", " special_chars = True\n", "\n", - " return minuscule and capital_letter and numbers and special_chars\n", + " if minuscule and capital_letter and numbers and special_chars:\n", + " return True\n", + "\n", + " return False\n", "\n" ] }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -546,7 +549,7 @@ "text": [ "....................................................................................................\n", "----------------------------------------------------------------------\n", - "Ran 100 tests in 0.103s\n", + "Ran 100 tests in 0.040s\n", "\n", "OK\n" ]