Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasero committed Oct 8, 2024
1 parent 24e8c65 commit daa59d0
Show file tree
Hide file tree
Showing 14 changed files with 2,440 additions and 254 deletions.
95 changes: 95 additions & 0 deletions _sources/chapters/module-2/In-class_100324.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6b64190e-5dce-4574-ad0d-ddb5a5a2cc00",
"metadata": {},
"source": [
"## Instructions \n",
"* Complete Jupyter Notebook for tasks. Clearly show relevant work.\n",
"\n",
"* Before beginning to fill in the notebook, make sure you have written down your name in the first cell, as well as of any collaborators in case you have worked in groups.\n",
"\n",
"* Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel $\\rightarrow$ Restart) and then **run all cells** (in the menubar, select Cell $\\rightarrow$ Run All).\n",
"\n",
"* Make sure your changes have been changed and when ready, submit the jupyter notebook through Canvas. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "91980179-e320-4362-8941-3ef911ce7671",
"metadata": {},
"outputs": [],
"source": [
"NAME = \"\"\n",
"COLLABORATORS = \"\""
]
},
{
"cell_type": "markdown",
"id": "332218e0-c6bc-4c48-9fc6-7bf2294a273b",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "f08804f8-a3cb-4f48-82b2-068bb2c347ea",
"metadata": {},
"source": [
"**1. The following function aims at returning whether a given list of numbers contains at least one element divisible by 7, but it fails due to a bug. You can test this failing behaviour by passing, for example, the list: [10, 7, 13].**\n",
"\n",
"**Redefine the function correcting the bug. Test that it behaves correctly with several examples. In addition, in a separate markdown cell, explain why the function was failing. (3 points)**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ee7b6179-eca2-4fae-89b6-c2bc5db5863e",
"metadata": {},
"outputs": [],
"source": [
"def has_atleast_one_seven(nums):\n",
" \"\"\"Return whether the given list of numbers is lucky. A lucky list contains\n",
" at least one number divisible by 7.\n",
" \"\"\"\n",
" for num in nums:\n",
" if num % 7 == 0:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "markdown",
"id": "70fed6c8-c03a-4b5d-ba84-20f33a4c9870",
"metadata": {},
"source": [
"**2. Using a for loop and and if statements, print all the numbers between 10 and 1000 (including both sides) that are divisible by 7 and the sum of their digits is greater than 10, but only if the number itself is also odd. (2 points)**"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
247 changes: 247 additions & 0 deletions _sources/chapters/module-2/In-class_100324_sols.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6b64190e-5dce-4574-ad0d-ddb5a5a2cc00",
"metadata": {},
"source": [
"## Instructions \n",
"* Complete Jupyter Notebook for tasks. Clearly show relevant work.\n",
"\n",
"* Before beginning to fill in the notebook, make sure you have written down your name in the first cell, as well as of any collaborators in case you have worked in groups.\n",
"\n",
"* Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel $\\rightarrow$ Restart) and then **run all cells** (in the menubar, select Cell $\\rightarrow$ Run All).\n",
"\n",
"* Make sure your changes have been changed and when ready, submit the jupyter notebook through Canvas. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "91980179-e320-4362-8941-3ef911ce7671",
"metadata": {},
"outputs": [],
"source": [
"NAME = \"\"\n",
"COLLABORATORS = \"\""
]
},
{
"cell_type": "markdown",
"id": "332218e0-c6bc-4c48-9fc6-7bf2294a273b",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "f08804f8-a3cb-4f48-82b2-068bb2c347ea",
"metadata": {},
"source": [
"**1. The following function aims at returning whether a given list of numbers contains at least one element divisible by 7, but it fails due to a bug. You can test this failing behaviour by passing, for example, the list: [10, 7, 13].**\n",
"\n",
"**Redefine the function correcting the bug. Test that it behaves correctly with several examples. In addition, in a separate markdown cell, explain why the function was failing. (3 points)**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ee7b6179-eca2-4fae-89b6-c2bc5db5863e",
"metadata": {},
"outputs": [],
"source": [
"def has_atleast_one_seven(nums):\n",
" \"\"\"Return whether the given list of numbers is lucky. A lucky list contains\n",
" at least one number divisible by 7.\n",
" \"\"\"\n",
" for num in nums:\n",
" if num % 7 == 0:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e9c98609-e356-4f40-b1e3-b81488101022",
"metadata": {},
"outputs": [],
"source": [
"# Solution\n",
"def has_atleast_one_seven(nums):\n",
" \"\"\"Return whether the given list of numbers is lucky. A lucky list contains\n",
" at least one number divisible by 7.\n",
" \"\"\"\n",
" for num in nums:\n",
" if num % 7 == 0:\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "df21433c-eaec-4448-93b4-bf3fd6ce4719",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"has_atleast_one_seven([10,1,13,1,1,1,1,6,7])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2cf67aef-1518-49c4-97d2-dc888289945a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"has_atleast_one_seven([10,8,13,1,4,5,6,81,6])"
]
},
{
"cell_type": "markdown",
"id": "15b49768-ee34-4350-9342-73a27876f36c",
"metadata": {},
"source": [
"The bug was that in the original function, only one iteration was performed, because of both return in the if/else, which made you exit the function after the first iteration. We only need to exit the function early if we happen to hit an element divisible by 7. If there is not, we should carry on looping the list until the end. If that happens, that's becaus we don't have any element divisible by 7, and the function should return a False."
]
},
{
"cell_type": "markdown",
"id": "70fed6c8-c03a-4b5d-ba84-20f33a4c9870",
"metadata": {},
"source": [
"**2. Using a for loop and and if statements, print all the numbers between 10 and 1000 (including both sides) that are divisible by 7 and the sum of their digits is greater than 10, but only if the number itself is also odd. (2 points)**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4480d428-ee6e-4629-b298-e0001548828e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"49\n",
"77\n",
"119\n",
"147\n",
"175\n",
"189\n",
"245\n",
"259\n",
"273\n",
"287\n",
"329\n",
"357\n",
"371\n",
"385\n",
"399\n",
"427\n",
"455\n",
"469\n",
"483\n",
"497\n",
"525\n",
"539\n",
"553\n",
"567\n",
"581\n",
"595\n",
"609\n",
"623\n",
"637\n",
"651\n",
"665\n",
"679\n",
"693\n",
"707\n",
"735\n",
"749\n",
"763\n",
"777\n",
"791\n",
"805\n",
"819\n",
"833\n",
"847\n",
"861\n",
"875\n",
"889\n",
"903\n",
"917\n",
"931\n",
"945\n",
"959\n",
"973\n",
"987\n"
]
}
],
"source": [
"numbers = range(10, 1001) # You have to use 1001 here to include 1000\n",
"\n",
"for num in numbers:\n",
" if num % 7 == 0: # This condition checks if the number is divisible by 7\n",
" num_as_str = str(num) # convert the number to string and store this info in a new variable\n",
"\n",
" # This block now calculates the sum of the digits of num as a string \n",
" sum_digits = 0\n",
" for s in num_as_str: # This loop through its individual digit of num and update the sumation adding the iterated digit\n",
" sum_digits += int(s) # we have to convert the character back to an integer. \n",
"\n",
" # Now add a condition to print num, only if the sum of digits is greater than 10 and num is odd, i.e, not even.\n",
" if (sum_digits > 10) and (num % 2 != 0):\n",
" print(num)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
8 changes: 4 additions & 4 deletions _sources/chapters/module-3/031-errors_and_exceptions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@
"\n",
"Write a function check_positive_number(num) that:\n",
"\n",
"1 - Tries to check if a number is positive.\n",
"2 - If the number is negative, raise a ValueError with the message 'Number must be positive'.\n",
"3 - If no exception occurs, print 'The number is positive' inside the else block.\n",
"4 - Ensure that, regardless of whether an exception occurs, a final message 'Check complete' is printed using a finally block\n",
"1 - Tries to check if a number is positive. \n",
"2 - If the number is negative, raise a ValueError with the message 'Number must be positive'. \n",
"3 - If no exception occurs, print 'The number is positive' inside the else block. \n",
"4 - Ensure that, regardless of whether an exception occurs, a final message 'Check complete' is printed using a finally block.\n",
"\n",
"```"
]
Expand Down
Loading

0 comments on commit daa59d0

Please sign in to comment.