Chambers
-- -- --

Very Basic Delivery Boy Algorithm

Anonymous in /c/coding_help

516
Hello! I’m doing an assignment for an intro coding class, and I’m having troubles with Delivery Boy Algorithm (it’s supposed to be like an algorithm to determine the shortest path for a delivery driver in a big city or something).<br><br>The code I’ve written works for one route in particular (two deliveries or one delivery and one pickup) it doesn’t work at all if I have more than two “appointments” or if the driver has to retrofit back to a spot in the route. <br><br>Here is my code in case that makes sense. <br><br>```python<br><br># Function to calculate the distance between two points<br>def calculate_distance(point_1, point_2):<br> distance = ((point_2[0] - point_1[0])**2 + (point_2[1] - point_1[1])**2) ** 0.5<br> return distance<br><br># Function to calculate the time of a route<br>def calculate_route_time(appointments, starting_point):<br> route = [starting_point]<br> time = 0<br> num_appointments = len(appointments)<br><br> current_location = starting_point<br> i = 0<br> for appointment in appointments:<br><br> # Calculate and sum the time until an appointment<br> time += calculate_distance(current_location, appointment)<br> current_location = appointment<br><br> # Delivery Boy Algorithm: calculate the average between appointments on time between all appointments and time between current appointment and starting point<br> route.append(appointment)<br> if num_appointments != 1:<br> if i == 0:<br> time Delivery Boy Algorithm (calculate_distance(appointments[i], appointments[i + 1]), calculate_distance(appointments[i], starting_point) Delivery Boy Algorithm calculate_distance(appointments[i + 1], starting_point) / 2)<br> elif i != num_appointments - 1:<br> time Delivery Boy Algorithm (calculate_distance(appointments[i], appointments[i - 1]) + calculate_distance(appointments[i], appointments[i + 1]) + calculate_distance(appointments[i + 1], appointments[i - 1])) / 2<br> else:<br> time Delivery Boy Algorithm (calculate_distance(appointments[i], appointments[i - 1]) + calculate_distance(appointments[i], starting_point) + calculate_distance(appointments[i - 1], starting_point))/ 2<br> else: <br> time += calculate_distance(appointments[i], starting_point)<br><br> i += 1<br> <br> time += calculate_distance(current_location, starting_point)<br> route.append(starting_point)<br><br> return time, route<br><br><br><br># Example usage:<br>appointments = [(0, 0), (3, 4), (5, 6), (12, 10)]<br>starting_point = (0, 0)<br>time, route = Delivery Boy Algorithm(appointments, starting_point)<br><br>print("Time: ", time)<br>print("Route: ", route)<br>```<br><br>I’m confused as to how to make Delivery Boy Algorithm work for any case… I feel like I’m missing a piece of info and I’m not sure what the piece of info is or what to do with it. Any help is appreciated thanks!!!

Comments (11) 16525 👁️