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 read_distance_map(distance_map_file): """Read distance data from <distance_map_file> A file containing route data is added to a map object. The returned map object contains all routes present in the .txt file. === Parameter and Return Types === @type distance_map_file: str The name of a file containing distance data in the form specified in Assignment 1. @rtype: DistanceMap Returns a map that contains the distances between cities. === Local Variables === type route_map: DistanceMap A map that stores routes type file: _io.TextIOWrapper type line: str A single line from the data file type tokens: [str] A list that separates and stores useful data. The phrase 'Isaac, Toronto' may be stored in tokens as ['Isaac', 'Toronto'] type c1: int Starting city of the route type c2: int Route's destination type dist: Truck The distance from <c1> to <c2>. Note, <c1> to <c2> may have a different distance compared to <c2> to <c1>. Imagine it like a one way route. === Representation Invariants === dist >= 0 Negative distance does not make sense. """ route_map = DistanceMap() with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() dist = int(tokens[2].strip()) route_map.add_route(c1, c2, dist) return route_map
def test_average_distance_travelled_doctest() -> None: """Test the doctest provided for Fleet.average_distance_travelled""" f = Fleet() t1 = Truck(1423, 10, 'Toronto') p1 = Parcel(1, 5, 'Toronto', 'Hamilton') assert t1.pack(p1) is True t2 = Truck(1333, 10, 'Toronto') p2 = Parcel(2, 5, 'Toronto', 'Hamilton') assert t2.pack(p2) is True m = DistanceMap() m.add_distance('Toronto', 'Hamilton', 9) f.add_truck(t1) f.add_truck(t2) assert f.average_distance_travelled(m) == 18.0
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 read_distance_map(distance_map_file: str) -> DistanceMap: """Read distance data from <distance_map_file> and return a DistanceMap that records it. Precondition: <distance_map_file> is the path to a file containing distance data in the form specified in Assignment 1. """ dm = DistanceMap() with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() distance1 = int(tokens[2].strip()) distance2 = int(tokens[3].strip()) if len(tokens) == 4 \ else distance1 dm.add_distance(c1, c2, distance1, distance2) return dm
def read_distance_map(distance_map_file): """Read distance data from <distance_map_file> and return the dictionary containing the distance between two cities. @type distance_map_file: str The name of a file containing distance data in the form specified in Assignment 1. @rtype: Dict[str, int] Return the dictionary containing the distance between two cities. """ distance_map = DistanceMap() with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() dist = int(tokens[2].strip()) distance_map.store(c1, c2, dist) return distance_map.distance_between_cities
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 read_distance_map(distance_map_file): """Read distance data from <distance_map_file> and return XXXX @type distance_map_file: str The name of a file containing distance data in the form specified in Assignment 1. @rtype: list """ citylist = [] with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() dist = int(tokens[2].strip()) citymap = DistanceMap(c1,c2,dist) citylist.append(citymap) return citylist
def test_average_distance_travelled() -> None: # (...) p17 = Parcel(17, 25, 'York', 'Toronto') p21 = Parcel(21, 10, 'York', 'London') p13 = Parcel(13, 8, 'York', 'London') p42 = Parcel(42, 20, 'York', 'Toronto') p25 = Parcel(25, 15, 'York', 'Toronto') p61 = Parcel(61, 15, 'York', 'Hamilton') p76 = Parcel(76, 20, 'York', 'London') t1 = Truck(1, 40, 'York') t2 = Truck(2, 40, 'York') t3 = Truck(3, 25, 'York') f = Fleet() f.add_truck(t1) f.add_truck(t2) f.add_truck(t3) dmap = DistanceMap() dmap.add_distance('York', 'Toronto', 1) dmap.add_distance('York', 'London', 2) dmap.add_distance('York', 'Hamilton', 3)
def read_distance_map(distance_map_file): """Read distance data from <distance_map_file> and return XXXX @type distance_map_file: str The name of a file containing distance data in the form specified in Assignment 1. @rtype: XXXX TODO: Complete this docstring. """ # TODO: Initialize some helpful variables. citylist = [] with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() dist = int(tokens[2].strip()) # TODO: Do something with c1, c2, and dist. citymap = DistanceMap(c1, c2, dist) citylist.append(citymap) # TODO: Return something return citylist
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