Skip to content
Open
Show file tree
Hide file tree
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
208 changes: 123 additions & 85 deletions your-code/main.ipynb → ...andling] - David Morazzo-checkpoint.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,82 +33,86 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 2,
"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",
" \"\"\"\n",
" This function will take any real number and \n",
" return the square root of its magnitude.\n",
" \n",
" Input: Real number\n",
" Output: Real number\n",
" \n",
" Sample Input: -4\n",
" Sample Output: 2.0\n",
" \"\"\"\n",
" \n",
" if x < 0:\n",
" x = abs(x)\n",
" return math.sqrt(x)\n",
"\n",
"sqrt_for_all(-1)"
"sqrt_for_all(-1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Modify the code below to handle zero as well. In the case of zero, return zero\n",
"\n",
"def divide(x, y):\n",
" \"\"\"\n",
" This function will take any two real numbers \n",
" and return their quotient. \n",
" If the denominator is zero, we return zero.\n",
" \n",
" Input: Real number\n",
" Output: Real number\n",
" \n",
" 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)"
"divide(5, 0)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[11]"
]
},
"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",
"# Otherwise, add the number to every element of the list and return the resulting list\n",
"\n",
"def add_elements(a, l):\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",
" of length one that is the sum of the numbers.\n",
" \n",
" Input: number and list or two numbers\n",
" Output: list\n",
" \n",
" Sample Input: 5, 6\n",
" Sample Output: [11]\n",
" \"\"\"\n",
" \n",
" return [a + element for element in l]\n",
" if not isinstance(l, list):\n",
" l = [l]\n",
" return [a + element for element in l];\n",
" \n",
"add_elements(5, 6)"
"add_elements(5, 6)\n"
]
},
{
Expand All @@ -122,29 +126,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])\n"
]
},
{
"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))\n"
]
},
{
Expand All @@ -158,47 +184,59 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Log Square of 5: 3.2188758248682006\n",
"Log Square of 2: 1.3862943611198906\n"
]
}
],
"source": [
"def log_square(x):\n",
" \"\"\"\n",
" This function takes a numeric value and returns the \n",
" natural log of the square of the number.\n",
" The function raises an error if the number is equal to zero.\n",
" Use the math.log function in this funtion.\n",
" \n",
" Input: Real number\n",
" Output: Real number or error\n",
" \n",
" Sample Input: 5\n",
" Sample Output: 3.21887\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" if x == 0:\n",
" raise ValueError(\"The input cannot be zero\")\n",
" return math.log(x ** 2)\n",
"\n",
"print(f\"Log Square of 5: {log_square(5)}\")\n",
"print(f\"Log Square of 2: {log_square(2)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n"
]
}
],
"source": [
"def check_capital(x):\n",
" \"\"\"\n",
" This function returns true if the string contains \n",
" at least one capital letter and throws an error otherwise.\n",
" \n",
" Input: String\n",
" Output: Bool or error message\n",
" \n",
" Sample Input: 'John'\n",
" Sample Output: True\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" if any(y.isupper() for y in x):\n",
" return True\n",
" else:\n",
" raise ValueError(\"The string does not contain at least one upper case letter\")\n",
" \n",
"print(check_capital('David'))\n",
"print(check_capital('Morazzo'))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -217,7 +255,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.3"
}
},
"nbformat": 4,
Expand Down
Loading