I took a code test for acme transport company and was rejected. That's ok. I'm still learning how to code. I didn't get much feedback so that's a bit frustrating. Can anyone here provide tips on how to optimize this python code? I thought that I had done a pretty good job with it, had the correct answers, etc. But apparently there are some fatal flaws, at least according to acme transport co!
Here's the challenge:
Parse a text file so that we know the following:
- how many cars were dropped in total?
- which car had the longest single trip? How long was the trip?
- what car had the longest cumulative distance? How long was that.
The text file is formatted like this:
timestamp, Integer, The time in seconds since the start of the simulation
id, String, The id of the associated vehicle, e.g. JK5T
event_type, String The type of the event is one of START_RIDE, END_RIDE, DROP
x, Double, The x coordinate of the location of where the event happened in the simulation
y, Double, The y coordinate of the location of where the event happened in the simulation
user_id, Integer, The id of the associated user or NULL if the event does not have an associated user
The first few lines of the file look like this:
302,OPN1,DROP,33.28280226855614,-87.88162047789511,NULL
305,OPN1,START_RIDE,33.28280226855614,-87.88162047789511,4204
419,G2I0,DROP,-28.367411419413685,23.567421582328606,NULL
426,OPN1,END_RIDE,7.563016232237587,91.97004351346015,4204
428,LQBF,DROP,-50.50579634246066,-54.53980771216895,NULL
441,5HQH,DROP,-70.05156581770649,36.8358644984674,NULL
For my code I import a library called shapely that helps determine distances:
import csv
from shapely.geometry import Point
from collections import defaultdict
def main():
myDict = defaultdict(dict)
aDict = {}
totalDrops = 0
maxCar = None
with open('events.txt') as f:
reader = csv.reader(f)
for row in reader:
if row[2] == 'DROP':
totalDrops += 1
myDict[row[1]]['drop'] = Point(float(row[3]), float(row[4]))
if row[2] == 'START_RIDE':
myDict[row[1]]['start_ride'] = Point(float(row[3]), float(row[4]))
if row[2] == 'END_RIDE':
singleTripDistance = myDict[row[1]]['drop'].distance(Point(float(row[3]), float(row[4])))
distanceTraveled = myDict[row[1]]['start_ride'].distance(Point(float(row[3]), float(row[4])))
aDict[row[1]] = singleTripDistance
if not maxCar:
maxCar = row[1]
if 'allDistances' not in myDict[row[1]]:
myDict[row[1]]['allDistances'] = distanceTraveled
else:
myDict[row[1]]['allDistances'] += distanceTraveled
if myDict[row[1]]['allDistances'] > myDict[maxCar]['allDistances']:
maxCar = row[1]
maxSingleTrip = max(zip(aDict.values(), aDict.keys()))
print('There are ' + '' + str(totalDrops) + ' ' + 'total Drops.' + '\n' + 'The Car that ends up furthest away from its drop is:' + ' ' + str(maxSingleTrip[1]) + ' ' + 'and the distance is:' + ' ' + str(maxSingleTrip[0]) + '\n' + 'The Car with the highest total distance traveled is:' + ' ' + maxCar + ', ' + 'and the total distance is:' + ' ' + str(myDict[maxCar]['allDistances']))
main()