From e9ae8b55e44b578e2a5435a24974772b96cc12c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Dias?= Date: Sat, 26 Nov 2022 18:15:32 +0000 Subject: [PATCH] Lab Done --- your-code/main.ipynb | 113 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 21 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 2a08c31..0e97436 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,13 +33,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:\n", "\n", "def sqrt_for_all(x):\n", + " if x < 0:\n", + " x = -x\n", + " return math.sqrt(x)\n", + " else:\n", + " return math.sqrt(x)\n", + "\n", " \"\"\"\n", " This function will take any real number and \n", " return the square root of its magnitude.\n", @@ -50,21 +67,32 @@ " Sample Input: -4\n", " Sample Output: 2.0\n", " \"\"\"\n", - " \n", - " return math.sqrt(x)\n", + " \n", "\n", "sqrt_for_all(-1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ "# Modify the code below to handle zero as well. In the case of zero, return zero\n", "\n", "def divide(x, y):\n", + " if y == 0:\n", + " print(0)\n", + " else:\n", + " return x / y\n", " \"\"\"\n", " This function will take any two real numbers \n", " and return their quotient. \n", @@ -76,24 +104,40 @@ " Sample Input: 5, 1\n", " Sample Output: 5.0\n", " \"\"\"\n", - " \n", - " return x / y\n", + "\n", + " \n", "\n", "divide(5, 0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 11]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modify the function below that it will take either an number and a list or two numbers. \n", "# If we take two numbers, add them together and return a list of length 1. \n", "# Otherwise, add the number to every element of the list and return the resulting list\n", "\n", "def add_elements(a, l):\n", + " if type(l) == list:\n", + " return [a + element for element in l]\n", + " else:\n", + " return [a + l]\n", " \"\"\"\n", + "\n", " This function takes either two numbers or a list and a number \n", " and adds the number to all elements of the list.\n", " If the function only takes two numbers, it returns a list \n", @@ -106,9 +150,9 @@ " Sample Output: [11]\n", " \"\"\"\n", " \n", - " return [a + element for element in l]\n", + " \n", " \n", - "add_elements(5, 6)" + "add_elements(5, [1,6])" ] }, { @@ -122,29 +166,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modify the code below:\n", "\n", "l = [1,2,3,4]\n", "\n", - "sum([element + 1 for element in l]" + "sum([element + 1 for element in l])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The current element in the loop is 1\n", + "The current element in the loop is 2\n", + "The current element in the loop is 3\n", + "The current element in the loop is 4\n" + ] + } + ], "source": [ "# Modify the code below:\n", "\n", "l = [1,2,3,4]\n", "\n", "for element in l:\n", - " print(\"The current element in the loop is\" + element)" + " print(f\"The current element in the loop is {element}\")" ] }, { @@ -203,7 +269,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.10.7 64-bit", "language": "python", "name": "python3" }, @@ -217,7 +283,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.7" + }, + "vscode": { + "interpreter": { + "hash": "d04cfb02d47fec9dc309eb1646b4ccc356a3d5933c6f9da519d88df5cd596b1d" + } } }, "nbformat": 4,