Question: from typing import List, Dict, Tuple from distance_map import DistanceMap class Parcel: # TODO: Implement this class! # It must be consistent


""" from typing import List, Dict, Tuple from distance_map import DistanceMap
class Parcel: """
""" # TODO: Implement this class! # It must be consistent with the Fleet class docstring examples below. pass
class Truck: """
""" # TODO: Implement this class! # It must be consistent with the Fleet class docstring examples below. pass
class Fleet: """ A fleet of trucks for making deliveries.
===== Public Attributes ===== trucks: List of all Truck objects in this fleet. """ trucks: List[Truck]
def __init__(self) -> None: """Create a Fleet with no trucks.
>>> f = Fleet() >>> f.num_trucks() 0 """ # TODO: Complete this method. pass
def add_truck(self, truck: Truck) -> None: """Add
Precondition: No truck with the same ID as
>>> f = Fleet() >>> t = Truck(1423, 1000, 'Toronto') >>> f.add_truck(t) >>> f.num_trucks() 1 """ # TODO: Complete this method. pass
# We will not test the format of the string that you return -- it is up # to you. def __str__(self) -> str: """Produce a string representation of this fleet """ # TODO: Complete this method. pass
def num_trucks(self) -> int: """Return the number of trucks in this fleet.
>>> f = Fleet() >>> t1 = Truck(1423, 10, 'Toronto') >>> f.add_truck(t1) >>> f.num_trucks() 1 """ # TODO: Complete this method. pass
def num_nonempty_trucks(self) -> int: """Return the number of non-empty trucks in this fleet.
>>> f = Fleet() >>> t1 = Truck(1423, 10, 'Toronto') >>> f.add_truck(t1) >>> p1 = Parcel(1, 5, 'Buffalo', 'Hamilton') >>> t1.pack(p1) True >>> p2 = Parcel(2, 4, 'Toronto', 'Montreal') >>> t1.pack(p2) True >>> t1.fullness() 90.0 >>> t2 = Truck(5912, 20, 'Toronto') >>> f.add_truck(t2) >>> p3 = Parcel(3, 2, 'New York', 'Windsor') >>> t2.pack(p3) True >>> t2.fullness() 10.0 >>> t3 = Truck(1111, 50, 'Toronto') >>> f.add_truck(t3) >>> f.num_nonempty_trucks() 2 """ # TODO: Complete this method. pass
def parcel_allocations(self) -> Dict[int, List[int]]: """Return a dictionary in which each key is the ID of a truck in this fleet and its value is a list of the IDs of the parcels packed onto it, in the order in which they were packed.
>>> f = Fleet() >>> t1 = Truck(1423, 10, 'Toronto') >>> p1 = Parcel(27, 5, 'Toronto', 'Hamilton') >>> p2 = Parcel(12, 5, 'Toronto', 'Hamilton') >>> t1.pack(p1) True >>> t1.pack(p2) True >>> t2 = Truck(1333, 10, 'Toronto') >>> p3 = Parcel(28, 5, 'Toronto', 'Hamilton') >>> t2.pack(p3) True >>> f.add_truck(t1) >>> f.add_truck(t2) >>> f.parcel_allocations() == {1423: [27, 12], 1333: [28]} True """ # TODO: Complete this method. pass
def total_unused_space(self) -> int: """Return the total unused space, summed over all non-empty trucks in the fleet. If there are no non-empty trucks in the fleet, return 0.
>>> f = Fleet() >>> f.total_unused_space() 0 >>> t = Truck(1423, 1000, 'Toronto') >>> p = Parcel(1, 5, 'Buffalo', 'Hamilton') >>> t.pack(p) True >>> f.add_truck(t) >>> f.total_unused_space() 995 """ # TODO: Complete this method. pass
def _total_fullness(self) -> float: """Return the sum of truck.fullness() for each non-empty truck in the fleet. If there are no non-empty trucks, return 0.
>>> f = Fleet() >>> f._total_fullness() == 0.0 True >>> t = Truck(1423, 10, 'Toronto') >>> f.add_truck(t) >>> f._total_fullness() == 0.0 True >>> p = Parcel(1, 5, 'Buffalo', 'Hamilton') >>> t.pack(p) True >>> f._total_fullness() 50.0 """ # TODO: Complete this method. pass
def average_fullness(self) -> float: """Return the average percent fullness of all non-empty trucks in the fleet.
Precondition: At least one truck is non-empty.
>>> f = Fleet() >>> t = Truck(1423, 10, 'Toronto') >>> p = Parcel(1, 5, 'Buffalo', 'Hamilton') >>> t.pack(p) True >>> f.add_truck(t) >>> f.average_fullness() 50.0 """ # TODO: Complete this method. pass
def total_distance_travelled(self, dmap: DistanceMap) -> int: """Return the total distance travelled by the trucks in this fleet, according to the distances in
Precondition:
>>> 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 """ # TODO: Complete this method. pass
def average_distance_travelled(self, dmap: DistanceMap) -> float: """Return the average distance travelled by the trucks in this fleet, according to the distances in
Include in the average only trucks that have actually travelled some non-zero distance.
Preconditions: -
>>> 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 """ # TODO: Complete this method. pass
if __name__ == '__main__': import python_ta python_ta.check_all(config={ 'allowed-import-modules': ['doctest', 'python_ta', 'typing', 'distance_map'], 'disable': ['E1136'], 'max-attributes': 15, }) import doctest doctest.testmod()
Task 2: Modelling the Domain Requires: knowing how to design and implement a class knowing how to identify appropriate classes from a problem description Your next task is, in file domain.py to define the classes necessary to represent the entities in the experiment: classes Parcel, Truck, and Fleet. We have designed the interface for class Fleet. As you might imagine, it keeps track of its trucks, but it also offers methods that report statistics about things such as how full its trucks are, on average. (These statistics are used when we create and run instances of the Experiment class.) Class Fleet is a client of classes Parcel and Truck, so use class Fleet to figure out what services it will need from classes Parcel and Truck. (A little bit of that is already dictated by the doctest examples in class Fleet.) Then use the Class Design Recipe to help you design classes Parcel and Truck to provide those services. Start each of these two classes simply, by focusing on the data you know it must store and any operations that you are certain it must provide. Very likely, as you start to use these classes in later steps, you will add new operations or make other changes. This is appropriate and a natural part of the design process. Once you have classes Parcel and Truck completed and well tested, you can move on to implement class Fleet. Remember to do all of your work for this task in the file domain.py. This module contains the classes required to represent the entities in the simulation: Parcel, Truck and Fleet. from typing import List, Dict, Tuple from distance_map import Distance Map class Parcel: # TODO: Implement this class! # It must be consistent with the Fleet class docstring examples below. pass class Truck # TODO: Implement this class! # It must be consistent with the Fleet class docstring examples below. pass class Fleet: A fleet of trucks for making deliveries. ===== Public Attributes ===== trucks: List of all Truck objects in this fleet. trucks: List[Truck) def __init__(self) -> None: Create a Fleet with no trucks. >>> f = Fleet() >>> fnum_trucks() O # TODO: Complete this method. pass def add_truck(self, truck: Truck) -> None: "Add
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
