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
249 changes: 249 additions & 0 deletions 1.-Python[Nathy]/1.-Snail-and-Well/snail-and-well.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
{
"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": 2,
"metadata": {},
"outputs": [],
"source": [
"well_height=125\n",
"daily_distance=30\n",
"nightly_distance=20\n",
"snail_position=[]"
]
},
{
"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": 3,
"metadata": {},
"outputs": [],
"source": [
"days=[]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Find the solution to the challenge using the variables defined above. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"snail_position=daily_distance-nightly_distance\n",
"days=well_height/snail_position"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Print the solution."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"El caracol saldrá del pozo luego de 12.5 días\n"
]
}
],
"source": [
"print(\"El caracol saldrá del pozo luego de\",days,\"días\")"
]
},
{
"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": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"El caracol saldrá del pozo luego de 6 días\n"
]
}
],
"source": [
"advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n",
"i=0\n",
"distance=0\n",
"vector_distance=[]\n",
"while distance<well_height:\n",
" advance=advance_cm[i]-nightly_distance\n",
" vector_distance.append(advance)\n",
" distance=distance+advance\n",
" i=i+1\n",
" \n",
" \n",
"\n",
"print(\"El caracol saldrá del pozo luego de\",i,\"días\")\n",
"\n",
" "
]
},
{
"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": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"La distancia máxima recorrida en un día por el caracol es de 57 cm\n",
"La distancia mínima recorrida en un día por el caracol es de 1 cm\n"
]
}
],
"source": [
" \n",
"print(\"La distancia máxima recorrida en un día por el caracol es de\",max(vector_distance),\"cm\")\n",
"print(\"La distancia mínima recorrida en un día por el caracol es de\",min(vector_distance),\"cm\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. What is its average progress? Take into account the snail slides at night."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"El promedio de avance del caracol es de 21.666666666666668 cm\n"
]
}
],
"source": [
"average=sum(vector_distance)/len(vector_distance)\n",
"print(\"El promedio de avance del caracol antes de salir del pozo es de \",average,\"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": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"La desviación estándar de desplazamiento del caracol en su recorrido por el pozo es de 19.510680835549195 cm\n"
]
}
],
"source": [
"import statistics\n",
"desvia=statistics.stdev(vector_distance)\n",
"print(\"La desviación estándar de desplazamiento del caracol en su recorrido por el pozo es de\",desvia,\"cm\")"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading