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
113 changes: 92 additions & 21 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,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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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])"
]
},
{
Expand All @@ -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}\")"
]
},
{
Expand Down Expand Up @@ -203,7 +269,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.10.7 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -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,
Expand Down