Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 149 additions & 23 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)"
]
},
Expand All @@ -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))"
]
},
{
Expand All @@ -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<ipython-input-18-1a88ad441441>\u001b[0m in \u001b[0;36m<module>\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<ipython-input-18-1a88ad441441>\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",
Expand All @@ -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<ipython-input-20-2c64822590cd>\u001b[0m in \u001b[0;36m<module>\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<ipython-input-20-2c64822590cd>\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",
Expand All @@ -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"
]
}
],
Expand All @@ -217,7 +343,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down