Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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