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,335 @@
{
"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": 93,
"metadata": {},
"outputs": [],
"source": [
"well_height = 125\n",
"daily_distance = 30\n",
"nightly_distance = 20\n",
"snail_position = 30"
]
},
{
"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": 94,
"metadata": {},
"outputs": [],
"source": [
"days = 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Find the solution to the challenge using the variables defined above. "
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"on day number 1 the snail_position value is 30\n",
"on day number 2 the snail_position value is 40\n",
"on day number 3 the snail_position value is 50\n",
"on day number 4 the snail_position value is 60\n",
"on day number 5 the snail_position value is 70\n",
"on day number 6 the snail_position value is 80\n",
"on day number 7 the snail_position value is 90\n",
"on day number 8 the snail_position value is 100\n",
"on day number 9 the snail_position value is 110\n",
"on day number 10 the snail_position value is 120\n",
"on day number 11 the snail_position value is 130\n"
]
}
],
"source": [
"print(\"on day number\", days, \"the snail_position value is\", snail_position, )\n",
"while snail_position <= 125:\n",
" snail_position = snail_position - nightly_distance + daily_distance\n",
" days += 1\n",
" print(\"on day number\", days, \"the snail_position value is\", snail_position, )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Print the solution."
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number 11\n"
]
}
],
"source": [
"print(\"counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number\", days)"
]
},
{
"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": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"on day 2 temporary position is 31\n",
"on day 3 temporary position is 44\n",
"on day 4 temporary position is 101\n",
"on day 5 temporary position is 125\n",
"on day 6 temporary position is 150\n",
"Day number where snails gets out is 6 and its theoretical position would have been 150\n"
]
}
],
"source": [
"advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n",
"\n",
"days = 1\n",
"snail_position = 30\n",
"iteration = 1\n",
"\n",
"while snail_position <= 125:\n",
" snail_position = snail_position - nightly_distance + advance_cm[iteration]\n",
" iteration += 1\n",
" days += 1\n",
" print(\"on day\", days, \"temporary position is\", snail_position)\n",
"print(\"Day number where snails gets out is\", days, \"and its theoretical position would have been\", snail_position)"
]
},
{
"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": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"maximum displacement in one day is 77 and the minimum displacement in one day is 12\n",
"total displacement (sum of total distance risen) is 206 cm\n",
"total displacement (including slidden distances) is 306 cm\n"
]
}
],
"source": [
"print(\"maximum displacement in one day is\", max(advance_cm), \"and the minimum displacement in one day is\", min(advance_cm))\n",
"\n",
"print(\"total displacement (sum of total distance risen) is\", sum(advance_cm[:5])+1, \"cm\")\n",
"\n",
"total_risen = sum(advance_cm[:5])+1\n",
"total_slid = nightly_distance * 5\n",
"\n",
"print(\"total displacement (including slidden distances) is\", total_risen + total_slid, \"cm\")\n",
"\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": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"day_number 1 \n",
"distance 30 \n",
"previous_day_distance saved for next day 30 \n",
"progress 30\n",
"day_number 2 \n",
"distance 21 \n",
"previous_day_distance saved for next day 21 \n",
"progress 1\n",
"day_number 3 \n",
"distance 33 \n",
"previous_day_distance saved for next day 33 \n",
"progress 13\n",
"day_number 4 \n",
"distance 77 \n",
"previous_day_distance saved for next day 77 \n",
"progress 57\n",
"day_number 5 \n",
"distance 44 \n",
"previous_day_distance saved for next day 44 \n",
"progress 24\n",
"day_number 6 \n",
"distance 45 \n",
"previous_day_distance saved for next day 45 \n",
"progress 25\n",
"[30, 1, 13, 57, 24, 25]\n",
"average progress is 25.0\n"
]
}
],
"source": [
"previous_day_distance = 0\n",
"nightly_distance = 0\n",
"progress = 0\n",
"days = 0\n",
"progress_array = []\n",
"\n",
"for distance in advance_cm[:6]:\n",
" progress = previous_day_distance - nightly_distance + distance - previous_day_distance\n",
" nightly_distance = 20\n",
" previous_day_distance = distance\n",
" days += 1\n",
" print(\"day_number\", days, \"\\ndistance\", distance, \"\\nprevious_day_distance saved for next day\", previous_day_distance, \"\\nprogress\", progress)\n",
" progress_array.append(progress)\n",
" \n",
"print(progress_array)\n",
"\n",
"average_progress_value = sum(progress_array)/len(progress_array)\n",
" \n",
"print(\"average progress is\", average_progress_value)"
]
},
{
"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": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[25.0, 576.0, 144.0, 1024.0, 1.0, 0.0]\n",
"variance 295.0\n",
"stardard variation is the square root of 295\n"
]
}
],
"source": [
"total_displacement = sum(progress_array)\n",
"distance_to_average = []\n",
"\n",
"for progress in progress_array:\n",
" distance_to_average.append((progress - average_progress_value)**2)\n",
"\n",
"print(distance_to_average)\n",
"\n",
"variance = sum(distance_to_average)/len(distance_to_average)\n",
"\n",
"print(\"variance\", variance)\n",
"\n",
"print(\"stardard variation is the square root of 295\")"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading