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
181 changes: 152 additions & 29 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,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Since -4 is negative the sqrt will be on the abs:\n",
"2.0\n",
"1.4142135623730951\n"
]
}
],
"source": [
"# Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:\n",
"\n",
Expand All @@ -50,17 +60,29 @@
" Sample Input: -4\n",
" Sample Output: 2.0\n",
" \"\"\"\n",
" \n",
" if x < 0:\n",
" print(f'Since {x} is negative the sqrt will be on the abs:')\n",
" return math.sqrt(abs(x))\n",
" return math.sqrt(x)\n",
"\n",
"sqrt_for_all(-1)"
"print(sqrt_for_all(-4))\n",
"print(sqrt_for_all(2.0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n",
"0\n"
]
}
],
"source": [
"# Modify the code below to handle zero as well. In the case of zero, return zero\n",
"\n",
Expand All @@ -76,17 +98,28 @@
" Sample Input: 5, 1\n",
" Sample Output: 5.0\n",
" \"\"\"\n",
" \n",
" if y == 0:\n",
" return 0\n",
" return x / y\n",
"\n",
"divide(5, 0)"
"print(divide(5, 1))\n",
"print(divide(5, 0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11]\n",
"[6, 7]\n"
]
}
],
"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 @@ -105,10 +138,19 @@
" Sample Input: 5, 6\n",
" Sample Output: [11]\n",
" \"\"\"\n",
" \n",
" return [a + element for element in l]\n",
"\n",
" # if type(l) == list:\n",
" # for element in l:\n",
" # new_list = a + element\n",
" # else:\n",
" # a + l\n",
" if type(l) == int:\n",
" return [ a + l ]\n",
" elif type(l) == list:\n",
" return [ a + element for element in l]\n",
" \n",
"add_elements(5, 6)"
"print(add_elements(5, 6))\n",
"print(add_elements(5, [1,2]))"
]
},
{
Expand All @@ -122,29 +164,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 5,
"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": 6,
"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,9 +222,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.2188758248682006\n"
]
},
{
"ename": "ValueError",
"evalue": "the number cannot be zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1832\\3780661581.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlog_square\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlog_square\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1832\\3780661581.py\u001b[0m in \u001b[0;36mlog_square\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;31m# Your code here:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"the number cannot be zero\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: the number cannot be zero"
]
}
],
"source": [
"def log_square(x):\n",
" \"\"\"\n",
Expand All @@ -176,14 +260,40 @@
" Sample Output: 3.21887\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" # Your code here:\n",
" if x == 0:\n",
" raise ValueError(\"the number cannot be zero\")\n",
" return math.log(x*x)\n",
"\n",
"print(log_square(5))\n",
"print(log_square(0))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
},
{
"ename": "Exception",
"evalue": "The text does not have any capital",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1832\\3367815293.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcheck_capital\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"John\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcheck_capital\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"john\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1832\\3367815293.py\u001b[0m in \u001b[0;36mcheck_capital\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The text does not have any capital\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 17\u001b[0m \u001b[1;31m# Your code here:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mException\u001b[0m: The text does not have any capital"
]
}
],
"source": [
"def check_capital(x):\n",
" \"\"\"\n",
Expand All @@ -196,14 +306,22 @@
" Sample Input: 'John'\n",
" Sample Output: True\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" for letter in x:\n",
" if letter.isupper():\n",
" return True\n",
" else:\n",
" raise Exception(\"The text does not have any capital\")\n",
" # Your code here:\n",
"\n",
"print(check_capital(\"John\"))\n",
"\n",
"print(check_capital(\"john\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.7.4 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -217,7 +335,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.4"
},
"vscode": {
"interpreter": {
"hash": "c438be70fe7b14fa31e78cfaf571a5f956365162ba67af6fb1eb95bac3223110"
}
}
},
"nbformat": 4,
Expand Down