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
203 changes: 176 additions & 27 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": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -33,9 +33,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 12,
"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 @@ -50,17 +61,30 @@
" Sample Input: -4\n",
" Sample Output: 2.0\n",
" \"\"\"\n",
" \n",
" return math.sqrt(x)\n",
" if x >= 0:\n",
" return math.sqrt(x)\n",
" else:\n",
" return math.sqrt(abs(x))\n",
"\n",
"sqrt_for_all(-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 13,
"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 +101,30 @@
" 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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[11]"
]
},
"execution_count": 14,
"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 +144,11 @@
" Sample Output: [11]\n",
" \"\"\"\n",
" \n",
" return [a + element for element in l]\n",
" \n",
" if isinstance(l, list):\n",
" return [a + element for element in l]\n",
" else:\n",
" return [a + l]\n",
"\n",
"add_elements(5, 6)"
]
},
Expand All @@ -122,29 +163,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 15,
"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 \" + str(element))"
]
},
{
Expand All @@ -158,9 +221,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"3.2188758248682006"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def log_square(x):\n",
" \"\"\"\n",
Expand All @@ -175,15 +249,52 @@
" Sample Input: 5\n",
" Sample Output: 3.21887\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" if x == 0:\n",
" raise ValueError(\"The input value cannot be zero.\")\n",
" else:\n",
" return math.log(x**2)\n",
"\n",
"log_square(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "ValueError",
"evalue": "The input value cannot be zero.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn [21], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m log_square(\u001b[39m0\u001b[39m)\n",
"Cell \u001b[1;32mIn [17], 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 input value cannot be zero.\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\u001b[39m*\u001b[39m\u001b[39m*\u001b[39m\u001b[39m2\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: The input value cannot be zero."
]
}
],
"source": [
"log_square(0)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def check_capital(x):\n",
" \"\"\"\n",
Expand All @@ -196,9 +307,42 @@
" Sample Input: 'John'\n",
" Sample Output: True\n",
" \"\"\"\n",
" \n",
" # Your code here:"
" if not any(letter.isupper() for letter in x):\n",
" raise ValueError(\"The input string must contain at least one capital letter.\")\n",
" else:\n",
" return True\n",
"\n",
"check_capital('John')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "The input string must contain at least one capital letter.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn [22], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m check_capital(\u001b[39m'\u001b[39m\u001b[39mjohn\u001b[39m\u001b[39m'\u001b[39m)\n",
"Cell \u001b[1;32mIn [19], line 13\u001b[0m, in \u001b[0;36mcheck_capital\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 returns true if the string contains \u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[39mat least one capital letter and throws an error otherwise.\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[39mSample Output: True\u001b[39;00m\n\u001b[0;32m 11\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 12\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39many\u001b[39m(letter\u001b[39m.\u001b[39misupper() \u001b[39mfor\u001b[39;00m letter \u001b[39min\u001b[39;00m x):\n\u001b[1;32m---> 13\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe input string must contain at least one capital letter.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 14\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 15\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mTrue\u001b[39;00m\n",
"\u001b[1;31mValueError\u001b[0m: The input string must contain at least one capital letter."
]
}
],
"source": [
"check_capital('john')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -217,7 +361,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.10.7"
},
"vscode": {
"interpreter": {
"hash": "721db305ef1fd1fc91cdf20e400af694a949fe540ac5f48c160f31c7e384879d"
}
}
},
"nbformat": 4,
Expand Down