{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "49bee2fa-9129-4c47-8401-aee2e75f538f",
"metadata": {},
"outputs": [],
"source": [
"#set up strings and outer dict\n",
"distance = 'distance'\n",
"gas = 'gas'\n",
"mileage = 'mileage'\n",
"starttime = 'start'\n",
"endtime = 'end'\n",
"triptime = 'time'\n",
"speed = 'speed'\n",
"fmt = '%H:%M:%S'\n",
"data = {}\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ed304d7d-dd82-430e-ad90-43b1eb9d5e76",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#read values for all cars and trips using with block and loops\n",
"import datetime as dt\n",
"import os\n",
"\n",
"filename = os.path.join('files','cardata.csv')\n",
"with open(filename) as f:\n",
" for line in f.read().splitlines():\n",
" fields = line.split(',')\n",
" carnum,start,end,d,g = tuple(fields)\n",
" #using setdefault makes this work whether we've seen this car yet or not\n",
" carinfo = data.setdefault(carnum,{})\n",
" #add the values to carinfo - use setdefault again so this works the first trip and subsequent trips\n",
" carinfo.setdefault(starttime,[]).append(start)\n",
" carinfo.setdefault(endtime,[]).append(end)\n",
" carinfo.setdefault(distance,[]).append(float(d))\n",
" carinfo.setdefault(gas,[]).append(float(g))\n",
"len(data)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "562ab38d-4a1f-4a05-b73a-63193510efb4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Car 73 had 9.268 mpg and traveled 45.354 mph\n",
"Car 83 had 10.487 mpg and traveled 47.194 mph\n",
"Car 97 had 12.130 mpg and traveled 54.294 mph\n",
"Car 46 had 8.954 mpg and traveled 51.147 mph\n",
"Car 60 had 12.817 mpg and traveled 50.210 mph\n",
"Car 30 had 9.311 mpg and traveled 56.371 mph\n",
"Car 36 had 10.919 mpg and traveled 74.773 mph\n",
"Car 76 had 10.539 mpg and traveled 55.059 mph\n",
"Car 35 had 8.925 mpg and traveled 39.766 mph\n",
"Car 74 had 7.308 mpg and traveled 30.365 mph\n",
"Car 50 had 8.027 mpg and traveled 73.732 mph\n",
"Car 21 had 8.137 mpg and traveled 51.365 mph\n",
"Car 6 had 10.295 mpg and traveled 57.895 mph\n",
"Car 63 had 11.001 mpg and traveled 56.082 mph\n",
"Car 49 had 9.458 mpg and traveled 55.232 mph\n",
"Car 13 had 9.251 mpg and traveled 42.778 mph\n",
"Car 2 had 9.747 mpg and traveled 53.502 mph\n",
"Car 61 had 9.374 mpg and traveled 79.258 mph\n",
"Car 65 had 10.368 mpg and traveled 49.106 mph\n",
"Car 29 had 9.211 mpg and traveled 58.916 mph\n",
"Car 87 had 6.521 mpg and traveled 30.081 mph\n",
"Car 93 had 7.622 mpg and traveled 34.395 mph\n",
"Car 32 had 8.091 mpg and traveled 51.563 mph\n",
"Car 34 had 8.889 mpg and traveled 40.289 mph\n",
"Car 75 had 8.901 mpg and traveled 57.267 mph\n",
"Car 78 had 9.483 mpg and traveled 63.169 mph\n",
"Car 4 had 8.813 mpg and traveled 39.023 mph\n",
"Car 98 had 8.959 mpg and traveled 80.092 mph\n",
"Car 12 had 10.610 mpg and traveled 51.203 mph\n",
"Car 53 had 14.517 mpg and traveled 112.964 mph\n",
"Car 88 had 11.301 mpg and traveled 53.551 mph\n",
"Car 86 had 8.051 mpg and traveled 46.408 mph\n",
"Car 33 had 8.183 mpg and traveled 27.370 mph\n",
"Car 71 had 9.322 mpg and traveled 39.661 mph\n",
"Car 70 had 10.985 mpg and traveled 44.011 mph\n",
"Car 82 had 9.199 mpg and traveled 35.879 mph\n",
"Car 24 had 9.666 mpg and traveled 41.931 mph\n",
"Car 66 had 10.944 mpg and traveled 39.745 mph\n",
"Car 81 had 13.050 mpg and traveled 49.839 mph\n",
"Car 41 had 10.776 mpg and traveled 42.250 mph\n",
"Car 20 had 5.922 mpg and traveled 34.865 mph\n",
"Car 85 had 13.118 mpg and traveled 45.585 mph\n",
"Car 80 had 9.286 mpg and traveled 38.072 mph\n",
"Car 94 had 10.866 mpg and traveled 44.723 mph\n",
"Car 1 had 8.877 mpg and traveled 53.885 mph\n",
"Car 43 had 9.782 mpg and traveled 44.385 mph\n",
"Car 68 had 11.288 mpg and traveled 45.625 mph\n",
"Car 31 had 11.514 mpg and traveled 61.164 mph\n",
"Car 8 had 10.754 mpg and traveled 72.304 mph\n",
"Car 77 had 10.844 mpg and traveled 52.866 mph\n",
"Car 52 had 11.791 mpg and traveled 58.444 mph\n",
"Car 14 had 9.336 mpg and traveled 42.742 mph\n",
"Car 58 had 12.684 mpg and traveled 71.495 mph\n",
"Car 5 had 8.230 mpg and traveled 40.230 mph\n",
"Car 64 had 13.898 mpg and traveled 53.237 mph\n",
"Car 3 had 9.882 mpg and traveled 55.423 mph\n",
"Car 92 had 7.021 mpg and traveled 43.768 mph\n",
"Car 23 had 13.299 mpg and traveled 51.710 mph\n",
"Car 7 had 12.312 mpg and traveled 54.980 mph\n",
"Car 99 had 11.256 mpg and traveled 40.341 mph\n",
"Car 26 had 16.502 mpg and traveled 134.710 mph\n",
"Car 15 had 12.533 mpg and traveled 41.741 mph\n",
"Car 59 had 7.438 mpg and traveled 60.286 mph\n",
"Car 56 had 8.782 mpg and traveled 37.309 mph\n"
]
}
],
"source": [
"#compute mileages and speeds - use another loop \n",
"msg = 'Car {} had {:.3f} mpg and traveled {:.3f} mph'\n",
"for car,info in data.items():\n",
" #compute mileage\n",
" info[mileage] = sum(info[distance]) / sum(info[gas])\n",
" #compute elapsed times - need nested loop\n",
" info[triptime] = [dt.datetime.strptime(end,fmt) - dt.datetime.strptime(start,fmt) for start,end in zip(info[starttime],info[endtime])]\n",
" # note for computing speed:\n",
" # to sum timedeltas, you need to pass in the \"start\" value - an empty timedelta. otherwise, the default start value is 0 and you can't add int + timedelta \n",
" info[speed] = sum(info[distance]) / sum(info[triptime],dt.timedelta()).total_seconds() * 3600 \n",
" print(msg.format(car,info[mileage],info[speed]))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4627633c-e427-41f8-b8c8-0f3b66790c21",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9.2678382740628, 10.487441643389081, 12.129515301993987, 8.95448056307955, 12.816930442016861, 9.311244239951964, 10.91906162078995, 10.538626751836484, 8.925454413516096, 7.307643832912639, 8.027023832835118, 8.13729843633687, 10.295238417159313, 11.000891075974842, 9.457717903549916, 9.250918627112227, 9.74667621832905, 9.374311551478437, 10.368019898527008, 9.210822865168842, 6.520664055614896, 7.621656354082262, 8.09077013220114, 8.88911015749426, 8.900854097172573, 9.482977752638064, 8.8127583279688, 8.959080507129608, 10.610002557970924, 14.517086825926967, 11.30131304543871, 8.051456304394673, 8.182897958790594, 9.322311923248611, 10.984803768098361, 9.199025183078314, 9.666269035053624, 10.944086402025171, 13.04970358541416, 10.77585456392661, 5.922107441267172, 13.11838671422459, 9.286042078531937, 10.86616369598734, 8.87714766193277, 9.781692426798575, 11.287596393892372, 11.514078086848512, 10.754488649308115, 10.844491102385023, 11.791329331384997, 9.335555148671379, 12.683737933631312, 8.229583339260179, 13.898210219715532, 9.881991880431512, 7.020868934172597, 13.299196030408366, 12.312360166923396, 11.256014044302818, 16.501870966414533, 12.533378929369807, 7.438459021519613, 8.782363565468925]\n"
]
}
],
"source": [
"#display message showing which car had the best mileage\n",
"#first extract mileages into list\n",
"m = [info[mileage] for info in data.values()]\n",
"print(m)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "98f451fb-545a-4ff7-9f78-bb97ff49a7b5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16.501870966414533"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#get max value\n",
"maxval = max(m)\n",
"maxval"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "6dff7e60-5a5c-4083-b82f-ad7e06876001",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#find index of max value\n",
"maxpos = m.index(maxval)\n",
"maxpos"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7bffa69c-5875-41cf-be72-4c5d13fd15cd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Car 26 had the best mileage, and traveled 134.7101129618749 mph.'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#find key for index of max value and print message\n",
"maxcar = list(data.keys())[maxpos]\n",
"f'Car {maxcar} had the best mileage, and traveled {data[maxcar][speed]} mph.'\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5e970e35-05ef-4791-9b24-a2b0da8dffdb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[24, 6, 20, 15, 16, 7, 5, 13, 14, 13, 5, 9, 5, 5, 30, 16, 14, 9, 5, 5, 5, 10, 8, 12, 7, 14, 7, 15, 18, 6, 12, 13, 7, 8, 7, 16, 14, 20, 18, 8, 9, 13, 18, 16, 9, 5, 17, 13, 7, 8, 7, 15, 5, 14, 5, 5, 6, 6, 9, 6, 6, 5, 5, 15]\n"
]
}
],
"source": [
"#find car with the most trips\n",
"# first create a list of how many trips each car took\n",
"l = [len(info[distance]) for info in data.values()]\n",
"print(l) "
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "301da3b3-0339-408e-9816-3ce932cd74de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#get max\n",
"maxtrips = max(l)\n",
"maxtrips"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "d55336d1-448d-4d41-aecc-bd99f6d3e8b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#find index of max\n",
"maxtpos = l.index(maxtrips)\n",
"maxtpos"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "f9d6d122-e077-4382-918f-414a817451a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Car 49 had the most trips: 30'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#get key for index and print message\n",
"maxtcar = list(data.keys())[maxtpos]\n",
"f'Car {maxtcar} had the most trips: {maxtrips}'"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "109d2f7e-8462-49ad-8e3b-d351380acbc5",
"metadata": {},
"outputs": [],
"source": [
"#bonus: write csv with the speed and mileage of each car\n",
"with open(os.path.join('files','car_results.csv'),'w') as f:\n",
" for car,info in data.items():\n",
" f.write(f'{car},{info[speed]},{info[mileage]}\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfa1918c-035b-426d-960d-d2f901d11c92",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}