diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 2a08c31..dd59366 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,9 +33,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 3, + "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", @@ -51,16 +62,34 @@ " Sample Output: 2.0\n", " \"\"\"\n", " \n", - " return math.sqrt(x)\n", + " # condition if the value is negative\n", + " if x < 0:\n", + " return math.sqrt(-x)\n", + " \n", + " # else\n", + " else:\n", + " return math.sqrt(x)\n", "\n", + "# test function\n", "sqrt_for_all(-1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modify the code below to handle zero as well. In the case of zero, return zero\n", "\n", @@ -77,16 +106,32 @@ " Sample Output: 5.0\n", " \"\"\"\n", " \n", - " return x / y\n", + " # condition if y is zero\n", + " if y == 0:\n", + " return 0\n", + " else:\n", + " return x / y\n", "\n", + "# test function\n", "divide(5, 0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 7, 8, 9, 10]" + ] + }, + "execution_count": 4, + "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", @@ -106,8 +151,14 @@ " Sample Output: [11]\n", " \"\"\"\n", " \n", - " return [a + element for element in l]\n", + " # condition if the type of a is a list or if the type of l is a list\n", + " if (type(a) == list) or (type(l) == list):\n", " \n", + " return [a + element for element in l]\n", + " else:\n", + " return [a + l]\n", + "\n", + "# test function\n", "add_elements(5, 6)" ] }, @@ -122,29 +173,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 6, + "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": 9, "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(\"The current element in the loop is \" + str(element))" ] }, { @@ -158,10 +231,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ValueError", + "evalue": "Log 0 is undefined and is not a real number.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;31m# test function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0mlog_square\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mlog_square\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;31m# raise error with error message\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Log 0 is undefined and is not a real number.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0;31m# return log of square of number\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: Log 0 is undefined and is not a real number." + ] + } + ], "source": [ + "# import math library\n", + "import math\n", + "\n", "def log_square(x):\n", " \"\"\"\n", " This function takes a numeric value and returns the \n", @@ -176,15 +265,43 @@ " Sample Output: 3.21887\n", " \"\"\"\n", " \n", - " # Your code here:" + " # Your code here:\n", + " \n", + " # state a condition if the number is zero\n", + " if x == 0:\n", + " \n", + " # raise error with error message\n", + " raise ValueError(\"Log 0 is undefined and is not a real number.\")\n", + " \n", + " # return log of square of number\n", + " return math.log(x**2)\n", + "\n", + "# test function\n", + "log_square(0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ValueError", + "evalue": "The input string does not contain an upper case letter.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;31m# test function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0mcheck_capital\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"peter\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mcheck_capital\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The input string does not contain an upper case letter.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;31m# test function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: The input string does not contain an upper case letter." + ] + } + ], "source": [ + "# import regex library\n", + "import re\n", + "\n", "def check_capital(x):\n", " \"\"\"\n", " This function returns true if the string contains \n", @@ -196,8 +313,17 @@ " Sample Input: 'John'\n", " Sample Output: True\n", " \"\"\"\n", + " # regex expression for uppercase in word (zero or more word characters followed by upper case letter followed by zero or more word characters)\n", + " expression = \"\\w*[A-Z]\\w*\"\n", " \n", - " # Your code here:" + " # condition if string contains capital letter\n", + " if re.match(expression, x):\n", + " return True\n", + " else:\n", + " raise ValueError(\"The input string does not contain an upper case letter.\")\n", + "\n", + "# test function\n", + "check_capital(\"peter\")\n" ] } ], @@ -217,7 +343,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "nbformat": 4,