Question
Python Coding The Network class For the purposes of our yet-to-be-built GPS system, a network is something that has some (i.e., zero or more) cities
Python Coding
The Network class For the purposes of our yet-to-be-built GPS system, a network is something that has some (i.e., zero or more) cities and possibly some roads joining some of those cities. Your company insists that the Network class must use lists of City and Road objects internally to keep track of the cities and roads, and that these lists are to be named cities and roads. You will just have to live with this design decision. They are leaving most of the other implementation details to you, although the GPS team has given you strict specifications for the methods of the Network class. Complete the implementation of the Network class by writing code for the three unfin- ished methods (has city(), has road(), and add city(), and so forth), following the specifica- tions given in the comments. It should be possible to write the methods in the order they are listed in the file. In other words, no method should need to use a method you havent written yet. This will enable you to run the test suite after completing each method. Many of the tests for later methods will fail if earlier methods are not working correctly, so make sure each method passes its tests before moving on to the next one.
comments:
class Network: """ A Network contains 0 or more cities and 0 or more roads joining cities. """
def __init__(self): """ Initializes this network to be an empty network, with no cities or roads. """ self._cities = [] # list of City objects in this network self._roads = [] # list of Road objects in this network # DO NOT MODIFY THE TWO LINES ABOVE # You may add other attributes here if convenient.
def has_city(self, name): """ Returns True if there is a city with the given name in this network; returns False otherwise.
name: string returns: boolean """ return NotImplemented
def has_road(self, road): """Returns True if the given road is in this network; returns False otherwise.
road: tuple of two strings, like ("Whittier", "Las Vegas") returns: boolean """ return NotImplemented
def add_city(self, name, pop): """ Adds a city with the given name and population to this network. If there is already a city with the given name in this network, does nothing. Returns True if the city was added; otherwise, returns False.
Hint: If it is OK to add the given city, create a new city object with the given name and pop, and append it to the appropriate list.
name: string pop: int returns: boolean """ return NotImplemented
def add_road(self, road, length): """Adds the given road (with the given length) to this network. The parameter 'road' is a tuple of two strings (city names). If either city is not in this network, does nothing. If this network already has a road joining the given cities, does nothing. Returns True if the road was added; otherwise, returns False.
road: tuple of two strings returns: boolean """ return NotImplemented
def del_road(self, road): """ Deletes the given road from this network. Does not delete any cities! If the given road is not in this network, does nothing. Returns True if road was deleted, False otherwise.
road: tuple of two strings returns: boolean """ return NotImplemented
def del_city(self, city): """ Deletes the given city AND all roads that touch it. If city is not in network, does nothing. Returns True if city was deleted, False otherwise.
Hint: Make sure del_road() is working properly before writing this method. Use del_road() in this method when you need to delete a road.
city: string returns: boolean """ return NotImplemented
def population(self, city): """ Returns the population of the given city. If city is not in this network, returns None.
city: string returns: int, or None """ return NotImplemented
def length(self, road): """ Returns the length of the given road. If road is not in this network, returns None.
road: tuple of two strings returns: int, or None """ return NotImplemented
def cities(self): """ Returns a list of the cities in this network. (The entries in the list are the names of the cities.)
returns: list of strings """ return NotImplemented
def roads(self): """ Returns a list of the roads in this network.
returns: list of tuples. Each tuple in the list is of the form ("Whittier", "Las Vegas"). """ return NotImplemented
def neighbors(self, city): """ Returns a list of the cities that are connected to the given city by a road. Returns None if the given city is not in this network.
city: string returns: list of strings, or None """ return NotImplemented
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started