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
Binary file added .DS_Store
Binary file not shown.
Binary file added 1.-Python/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://bit.ly/2VnXWr2\" width=\"100\" align=\"left\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Snail and the Well\n",
"\n",
"A snail falls at the bottom of a 125 cm well. Each day the snail rises 30 cm. But at night, while sleeping, slides 20 cm because the walls are wet. How many days does it take for the snail to escape the well?\n",
"\n",
"**Hint**: The snail gets out of the well when it surpasses the 125cm of height.\n",
"\n",
"## Tools\n",
"\n",
"1. Loop: **while**\n",
"2. Conditional statements: **if-else**\n",
"3. Function: **print()**\n",
"\n",
"## Tasks\n",
"\n",
"#### 1. Assign the challenge data to variables with representative names: `well_height`, `daily_distance`, `nightly_distance` and `snail_position`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"well_height = 125\n",
"snail_position = 0\n",
"daily_distance = 30\n",
"nightly_distance = -20\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Create a variable `days` to keep count of the days that pass until the snail escapes the well. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"days = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Find the solution to the challenge using the variables defined above. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while snail_position <= well_height:\n",
" days += 1\n",
" snail_position += daily_distance\n",
" if snail_position <= well_height:\n",
" snail_position += nightly_distance\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Print the solution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('It took the snail', days, 'days', 'to escape the well.' )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bonus\n",
"The distance traveled by the snail each day is now defined by a list.\n",
"```\n",
"advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n",
"```\n",
"On the first day, the snail rises 30cm but during the night it slides 20cm. On the second day, the snail rises 21cm but during the night it slides 20cm, and so on. \n",
"\n",
"#### 1. How many days does it take for the snail to escape the well?\n",
"Follow the same guidelines as in the previous challenge.\n",
"\n",
"**Hint**: Remember that the snail gets out of the well when it surpasses the 125cm of height."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while snail_position <= well_height:\n",
" snail_position += advance_cm[days]\n",
" days += 1\n",
" if snail_position <= well_height:\n",
" snail_position += nightly_distance\n",
"\n",
"print('It took the snail', days, 'days', 'to escape the well.' )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. What is its maximum displacement in one day? And its minimum? Calculate the displacement using only the travel distance of the days used to get out of the well. \n",
"**Hint**: Remember that displacement means the total distance risen taking into account that the snail slides at night. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"displacement_cm = []\n",
"\n",
"for advance in advance_cm[:6]:\n",
" daily_displ = advance + nightly_distance\n",
" displacement_cm.append(daily_displ)\n",
" \n",
"print('Maimimum daily displacement:', max(displacement_cm), 'cm')\n",
"print('Minimum daily displacement:', min(displacement_cm), 'cm')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. What is its average progress? Take into account the snail slides at night."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"avg_displacement = sum(displacement_cm)/len(displacement_cm)\n",
"print('Average daily displacement:', avg_displacement, 'cm')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import statistics\n",
"\n",
"displacement_stdev = statistics.stdev(displacement_cm)\n",
"print('Standard deviation of the displacement:', displacement_stdev)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
58 changes: 49 additions & 9 deletions 1.-Python/1.-Snail-and-Well/snail-and-well.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"well_height = 125\n",
"snail_position = 0\n",
"daily_distance = 30\n",
"nightly_distance = -20\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -47,7 +52,9 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"days = 0"
]
},
{
"cell_type": "markdown",
Expand All @@ -61,7 +68,13 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"while snail_position <= well_height:\n",
" days += 1\n",
" snail_position += daily_distance\n",
" if snail_position <= well_height:\n",
" snail_position += nightly_distance\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -75,7 +88,9 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"print('It took the snail', days, 'days', 'to escape the well.' )"
]
},
{
"cell_type": "markdown",
Expand All @@ -99,7 +114,15 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"while snail_position <= well_height:\n",
" snail_position += advance_cm[days]\n",
" days += 1\n",
" if snail_position <= well_height:\n",
" snail_position += nightly_distance\n",
"\n",
"print('It took the snail', days, 'days', 'to escape the well.' )"
]
},
{
"cell_type": "markdown",
Expand All @@ -114,7 +137,16 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"displacement_cm = []\n",
"\n",
"for advance in advance_cm[:6]:\n",
" daily_displ = advance + nightly_distance\n",
" displacement_cm.append(daily_displ)\n",
" \n",
"print('Maimimum daily displacement:', max(displacement_cm), 'cm')\n",
"print('Minimum daily displacement:', min(displacement_cm), 'cm')\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -128,7 +160,10 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"avg_displacement = sum(displacement_cm)/len(displacement_cm)\n",
"print('Average daily displacement:', avg_displacement, 'cm')"
]
},
{
"cell_type": "markdown",
Expand All @@ -142,7 +177,12 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"import statistics\n",
"\n",
"displacement_stdev = statistics.stdev(displacement_cm)\n",
"print('Standard deviation of the displacement:', displacement_stdev)"
]
}
],
"metadata": {
Expand All @@ -161,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.9.4"
}
},
"nbformat": 4,
Expand Down
Loading