def test_distance_map_complex() -> None: """Testing complex applications of DistanceMap""" m = DistanceMap() m.add_distance('A', 'B', 10) assert m.distance('A', 'C') == -1 assert m.distance('A', 'A') == 0 assert m.distance('A', 'B') == 10 m.add_distance('A', 'B', 100) assert m.distance('A', 'B') == 100
def test_distance_map_basic() -> None: """Test DistanceMap when a single distance is provided.""" m = DistanceMap() assert m.distance('Montreal', 'Toronto') == -1 m.add_distance('Montreal', 'Toronto', 4) assert m.distance('Montreal', 'Toronto') == 4 assert m.distance('Toronto', 'Montreal') == 4 m.add_distance('Toronto', 'Montreal', 5) assert m.distance('Toronto', 'Montreal') == 5
def average_distance_travelled(self, dmap: DistanceMap) -> float: """Return the average distance travelled by the trucks in this fleet, according to the distances in <dmap>. Include in the average only trucks that have actually travelled some non-zero distance. Preconditions: - <dmap> contains all distances required to compute the average distance travelled. - At least one truck has travelled a non-zero distance. >>> f = Fleet() >>> t1 = Truck(1423, 10, 'Toronto') >>> p1 = Parcel(1, 5, 'Toronto', 'Hamilton') >>> t1.pack(p1) True >>> t2 = Truck(1333, 10, 'Toronto') >>> p2 = Parcel(2, 5, 'Toronto', 'Hamilton') >>> t2.pack(p2) True >>> from distance_map import DistanceMap >>> m = DistanceMap() >>> m.add_distance('Toronto', 'Hamilton', 9) >>> f.add_truck(t1) >>> f.add_truck(t2) >>> f.average_distance_travelled(m) 18.0 """ avg_distances = [] num_trucks = 0 for truck in self.trucks: # Make sure the truck has more than just the depot in its route if len(truck.route) > 1: s = 0 # Loop through all cities in route for i in range(len(truck.route[1:])): start_city = truck.route[i - 1] end_city = truck.route[i] # Make sure distance is stored s += dmap.distance(start_city, end_city) # Add distance from final city to depot s += dmap.distance(truck.route[-1], truck.route[0]) avg_distances.append(s) num_trucks += 1 avg = sum(avg_distances) / num_trucks return round(avg, 2)
def total_distance_travelled(self, dmap: DistanceMap) -> int: """Return the total distance travelled by the trucks in this fleet, according to the distances in <dmap>. Precondition: <dmap> contains all distances required to compute the average distance travelled. >>> f = Fleet() >>> t1 = Truck(1423, 10, 'Toronto') >>> p1 = Parcel(1, 5, 'Toronto', 'Hamilton') >>> t1.pack(p1) True >>> t2 = Truck(1333, 10, 'Toronto') >>> p2 = Parcel(2, 5, 'Toronto', 'Hamilton') >>> t2.pack(p2) True >>> from distance_map import DistanceMap >>> m = DistanceMap() >>> m.add_distance('Toronto', 'Hamilton', 9) >>> f.add_truck(t1) >>> f.add_truck(t2) >>> f.total_distance_travelled(m) 36 """ s = 0 for truck in self.trucks: # Make sure the truck has more than just the depot in its route if len(truck.route) > 1: # Loop through all cities in route for i in range(len(truck.route[1:])): start_city = truck.route[i - 1] end_city = truck.route[i] # Make sure distance is stored s += dmap.distance(start_city, end_city) # Add distance from final city to depot. s += dmap.distance(truck.route[-1], truck.route[0]) return s
def test_add_distance_different_back_forth() -> None: m = DistanceMap() m.add_distance("Montreal", "Toronto", 115, 100) assert m.distance("Montreal", "Toronto") == 115 assert m.distance("Toronto", "Montreal") == 100