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
172 changes: 172 additions & 0 deletions bus/poad_solution_bus.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bus\n",
"\n",
"This bus has a passenger entry and exit control system to monitor the number of occupants it carries and thus detect when there is too high a capacity.\n",
"\n",
"At each stop the entry and exit of passengers is represented by a tuple consisting of two integer numbers.\n",
"```\n",
"bus_stop = (in, out)\n",
"```\n",
"The succession of stops is represented by a list of these tuples.\n",
"```\n",
"stops = [(in1, out1), (in2, out2), (in3, out3), (in4, out4)]\n",
"```\n",
"\n",
"## Goals:\n",
"* lists, tuples\n",
"* while/for loops\n",
"* minimum, maximum, length\n",
"* average, standard deviation\n",
"\n",
"## Tasks\n",
"1. Calculate the number of stops.\n",
"2. Assign to a variable a list whose elements are the number of passengers at each stop (in-out),\n",
"3. Find the maximum occupation of the bus.\n",
"4. Calculate the average occupation. And the standard deviation.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# variables\n",
"#stops = [(on1, off1), (on2, off2), (on3, off3), (on4, off4)]\n",
"stops = [(2,0), (1,2), (5,3), (4,2)]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"# 1. Calculate the number of stops.\n",
"print(len(stops))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Passengers entering [2, 1, 5, 4]\n",
"Passengers exiting [0, 2, 3, 2]\n",
"Number of passengers at each stop is [2, 1, 3, 5]\n"
]
}
],
"source": [
"stop_list_on = []\n",
"stop_list_off = []\n",
"for i in stops:\n",
" stop_list_on.append(i[0])\n",
" stop_list_off.append(i[1])\n",
"print(\"Passengers entering \", stop_list_on)\n",
"print(\"Passengers exiting \", stop_list_off)\n",
"\n",
"# 2. Assign a variable a list whose elements are the number of passengers in each stop:\n",
"bus_occupancy = []\n",
"stop_occupancy = 0\n",
"for i in range(len(stop_list_on)):\n",
" stop_occupancy += (stop_list_on[i] - stop_list_off[i])\n",
" bus_occupancy.append(int(stop_occupancy)) \n",
"print(\"Number of passengers at each stop is\", bus_occupancy)\n",
"# Each item depends on the previous item in the list + in - out.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"# 3. Find the maximum occupation of the bus.\n",
"print(max(bus_occupancy))\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average occupancy 2.75\n",
"Standard deviation of the occupancy is 1.479019945774904\n"
]
}
],
"source": [
"# 4. Calculate the average occupation. And the standard deviation.\n",
"avg_occupancy = (sum(bus_occupancy) / len(bus_occupancy))\n",
"print(\"Average occupancy\", avg_occupancy)\n",
"\n",
"summation = 0\n",
"for i in bus_occupancy:\n",
" summation += (i - avg_occupancy) ** 2\n",
"std_dev = (summation / len(bus_occupancy)) ** 0.5\n",
"print(\"Standard deviation of the occupancy is\", std_dev)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading