diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 2a08c31..cef7d9f 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,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.0\n" + ] + } + ], "source": [ "# Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:\n", "\n", @@ -50,17 +58,28 @@ " Sample Input: -4\n", " Sample Output: 2.0\n", " \"\"\"\n", + " if x < 0:\n", + " x = x*(-1)\n", " \n", " return math.sqrt(x)\n", "\n", - "sqrt_for_all(-1)" + "result = sqrt_for_all(-1)\n", + "print(result)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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", @@ -76,17 +95,32 @@ " Sample Input: 5, 1\n", " Sample Output: 5.0\n", " \"\"\"\n", - " \n", - " return x / y\n", + " if y == 0:\n", + " return 0\n", + " else:\n", + " return x / y\n", "\n", - "divide(5, 0)" + "result = divide(5, 0)\n", + "\n", + "print(result)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[11]" + ] + }, + "execution_count": 13, + "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", @@ -105,8 +139,11 @@ " Sample Input: 5, 6\n", " Sample Output: [11]\n", " \"\"\"\n", - " \n", - " return [a + element for element in l]\n", + " if type(l) == list: \n", + " return [a + element for element in l]\n", + " else:\n", + " c = a + l\n", + " return [c]\n", " \n", "add_elements(5, 6)" ] @@ -122,29 +159,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 14, + "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": 16, "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\" , element)" ] }, { @@ -158,9 +217,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ValueError", + "evalue": "The number entered is equal to 0!", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn [26], line 20\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m math\u001b[39m.\u001b[39mlog(x)\n\u001b[0;32m 18\u001b[0m \u001b[39m# Your code here:\u001b[39;00m\n\u001b[1;32m---> 20\u001b[0m \u001b[39mprint\u001b[39m(log_square(\u001b[39m0\u001b[39m))\n", + "Cell \u001b[1;32mIn [26], line 15\u001b[0m, in \u001b[0;36mlog_square\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39mThis function takes a numeric value and returns the \u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[39mnatural log of the square of the number.\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[39mSample Output: 3.21887\u001b[39;00m\n\u001b[0;32m 13\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 14\u001b[0m \u001b[39mif\u001b[39;00m x \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m:\n\u001b[1;32m---> 15\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe number entered is equal to 0!\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 16\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m math\u001b[39m.\u001b[39mlog(x)\n", + "\u001b[1;31mValueError\u001b[0m: The number entered is equal to 0!" + ] + } + ], "source": [ "def log_square(x):\n", " \"\"\"\n", @@ -175,16 +247,37 @@ " Sample Input: 5\n", " Sample Output: 3.21887\n", " \"\"\"\n", - " \n", - " # Your code here:" + " if x == 0:\n", + " raise ValueError(\"The number entered is equal to 0!\")\n", + " else:\n", + " return math.log(x)\n", + " # Your code here:\n", + "\n", + "print(log_square(0))\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ValueError", + "evalue": "The string doesnt contain any capital letters", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn [30], line 25\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 23\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe string doesnt contain any capital letters\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 25\u001b[0m \u001b[39mprint\u001b[39m(check_capital(\u001b[39m\"\u001b[39m\u001b[39mbanana\u001b[39m\u001b[39m\"\u001b[39m))\n", + "Cell \u001b[1;32mIn [30], line 23\u001b[0m, in \u001b[0;36mcheck_capital\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 21\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mTrue\u001b[39;00m\n\u001b[0;32m 22\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m---> 23\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe string doesnt contain any capital letters\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[1;31mValueError\u001b[0m: The string doesnt contain any capital letters" + ] + } + ], "source": [ + "from numpy import true_divide\n", + "\n", + "\n", "def check_capital(x):\n", " \"\"\"\n", " This function returns true if the string contains \n", @@ -197,13 +290,22 @@ " Sample Output: True\n", " \"\"\"\n", " \n", - " # Your code here:" + " # Your code here:\n", + " import re\n", + "\n", + " for i in x:\n", + " if re.search('[A-Z]', i):\n", + " return True\n", + " else:\n", + " raise ValueError(\"The string doesnt contain any capital letters\")\n", + "\n", + "print(check_capital(\"banana\"))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.8.0 64-bit", "language": "python", "name": "python3" }, @@ -217,7 +319,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.0" + }, + "vscode": { + "interpreter": { + "hash": "c5b5e55a61251b9c21345703918cb7f79268e5ebdff672a62273f845ec6e3a46" + } } }, "nbformat": 4,