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
134 changes: 112 additions & 22 deletions 1.-Python/1.-Snail-and-Well/snail-and-well.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": []
"source": [
"well_height = 125\n",
"daily_distance = 30\n",
"nightly_distance = 20\n",
"snail_position = 0"
]
},
{
"cell_type": "markdown",
Expand All @@ -44,10 +49,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
"source": [
"days = 1"
]
},
{
"cell_type": "markdown",
Expand All @@ -58,10 +65,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": []
"source": [
"while snail_position < well_height:\n",
" snail_position += daily_distance\n",
" if snail_position <= well_height:\n",
" snail_position -= nightly_distance\n",
" days += 1"
]
},
{
"cell_type": "markdown",
Expand All @@ -72,10 +85,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The snail takes 11 days to escape the well\n"
]
}
],
"source": [
"print(\"The snail takes\", days, \"days to escape the well\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -96,10 +119,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BONUS: The snail takes 6 days to escape the well\n"
]
}
],
"source": [
"days = 1\n",
"snail_position = 0\n",
"advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n",
"while snail_position < well_height:\n",
" snail_position += advance_cm[days-1]\n",
" if snail_position <= well_height:\n",
" snail_position -= nightly_distance\n",
" days += 1\n",
"print(\"BONUS: The snail takes\", days, \"days to escape the well\")"
]
},
{
"cell_type": "markdown",
Expand All @@ -111,10 +152,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The maximum displacement in one day is 57\n",
"The minimum displacement in one day is 1\n"
]
}
],
"source": [
"# Accumulated displacements before the last day\n",
"accumDisp = sum(advance_cm[:days-1]) - nightly_distance*(days-1)\n",
"# Snail's displacement on the last day\n",
"lastDayDisp = well_height - accumDisp\n",
"# Compute the maximum and minimum (no slide for the last day)\n",
"maxDailyDisp = max([max(advance_cm[:days-1])-nightly_distance,lastDayDisp])\n",
"minDailyDisp = min([min(advance_cm[:days-1])-nightly_distance,lastDayDisp])\n",
"print(\"The maximum displacement in one day is\", maxDailyDisp)\n",
"print(\"The minimum displacement in one day is\", minDailyDisp)"
]
},
{
"cell_type": "markdown",
Expand All @@ -125,10 +185,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The average displacement is 20.83\n"
]
}
],
"source": [
"# Sum of displacements is well height\n",
"meanDisp = well_height/days\n",
"print(\"The average displacement is\", round(meanDisp,2))"
]
},
{
"cell_type": "markdown",
Expand All @@ -139,10 +211,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The standard deviation of displacements is 17.83\n"
]
}
],
"source": [
"# Store the accumulated square distance to mean\n",
"accumDist2Mean = 0\n",
"for day in range(days):\n",
" if day < days:\n",
" accumDist2Mean += (advance_cm[day]-nightly_distance-meanDisp)**2\n",
" else:\n",
" accumDist2Mean += (lastDayDisp-meanDisp)**2\n",
"stdDev = (accumDist2Mean/days)**(0.5)\n",
"print(\"The standard deviation of displacements is\", round(stdDev,2))"
]
}
],
"metadata": {
Expand All @@ -161,7 +251,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
Loading