Question: Python Coding You are going to want to write your classes in a general way so they can be used to describe any network of

Python Coding

You are going to want to write your classes in a general way so they can be used to describe any network of cities and roads. Your company has provided specifications for three classes: City, Road, and Network (in the file assign6.py), and they have given you a test suite for these classes (in test assign6.py). Many of the unit tests in the test suite use a fictional network of roads and cities on a fantastic island.

The City and Road classes are very simple, so you will implement them first, before tackling the Network class, which uses City and Road objects internally.

Problem 6A: The City class

We will view a city as something that has a unique name and a population. Complete the implementation of the City class by writing code for the three unfinished methods ( eq (), set pop(), and str ()), following the specifications given in the comments.

Test your City methods by running test assign6.py. Once you have passed all the City tests, move on to the Road class.

Problem 6B: The Road class

For us, a road will be something that has a length and connects two different cities. Complete the implementation of the Road class by writing code for the three unfinished methods ( eq (), set length(), and str ()), following the specifications given in the comments.

Test your Road methods by running test assign6.py. You should now see all of the City and Road tests pass.

Problem 6C: 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 unfinished methods (has city(), has road(), and add city(), and so forth), following the specifications 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.

After your code passes all of the City, Road, and Network unit tests, you are finished with assign6.py and you can move on.

Problem 6D: Some client code that uses the Network class

Before shipping your completed classes off to the GPS team, you decide to test-drive your Network class yourself. In assign6 client.py, you will find a function called create island net(), which creates and returns a Network object representing the mythical island shown above. Write a second function, print network(), that takes a Network object as a parameter and prints out a listing of all the cities (and their populations) in the given network, and for each city, a listing of the cities to which it is connected (and the distance to each). Format the listing so that when you execute the code shown below in the interpreter, it looks identical to the listing shown below:

>>> fantasy_island = create_island_net()

>>> print_cities(fantasyIsland)

Lilliput has 20000 people and is connected to:

Scranton (11 miles)

Gatorland (30 miles)

Adventureland (18 miles)

Scranton has 76000 people and is connected to:

Lilliput (11 miles)

Emerald City (6 miles)

Gatorland has 60 people and is connected to:

Lilliput (30 miles)

Adventureland (8 miles)

Emerald City (9 miles)

Adventureland has 3060 people and is connected to:

Lilliput (18 miles)

Gatorland (8 miles)

Ghosttown (9 miles)

Ghosttown has 0 people and is connected to:

Adventureland (9 miles)

Emerald City has 100000 people and is connected to:

Scranton (6 miles)

Gatorland (9 miles)

Optional Challenge Problem If you got this far, congratulations! But you might have noticed that there is another unfinished client function, find path(), in assign6 client.py. If you want a challenge (and concomitant extra credit), implement this function so that it behaves according to the specification in the file.

================================================================================================================

assign6.py

==================================================================================================================

class City:

"""

A City has a name and a population.

"""

def __init__(self, name, pop):

self.name = str(name)

self.pop = int(pop)

def __eq__(self, other):

"""Two cities should be equal if they have the same name. Returns

True if self and other are equal, False otherwise.

other: City object

"""

return NotImplemented

def __ne__(self, other):

# DO NOT MODIFY THIS METHOD

return not (self == other)

def set_pop(self, pop):

"""Sets this city's pop to the given pop. Does not return

anything.

pop: int

"""

return NotImplemented

def __str__(self):

"""Returns a string (this city's name). DOES NOT PRINT ANYTHING"""

return "NotImplemented"

def __repr__(self):

# DO NOT MODIFY THIS METHOD

return str(self)

class Road:

"""

A Road connects two different cities and has a length.

"""

def __init__(self, city1, city2, length):

"""

city1, city2: strings

length: int

"""

# DO NOT MODIFY THIS METHOD

if city1 == city2:

raise ValueError("Cities must be different")

self.city1 = city1

self.city2 = city2

self.length = int(length)

def __eq__(self, other):

"""

Two roads are considered equal if they connect the same

two cities.

other: Road object

returns: boolean

"""

return NotImplemented

def __ne__(self, other):

# DO NOT MODIFY THIS METHOD

return not (self == other)

def set_length(self, length):

"""

Sets this road's length to the given length. Does not

return anything.

length: int

"""

return NotImplemented

def __str__(self):

"""

Returns a string representation of this road, of the form

DOES NOT PRINT ANYTHING.

returns: string

"""

return "Not Implemented"

def __repr__(self):

# DO NOT MODIFY THIS METHOD

return str(self)

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

==================================================================================================================

assign6_client.py

==================================================================================================================

from assign6 import Network

def create_island_net():

"""

Create and return the "island" network shown on the handout.

returns: a Network object

"""

# DO NOT MODIFY THIS METHOD

n = Network()

n.add_city("Lilliput", 20000)

n.add_city("Scranton", 76000)

n.add_city("Gatorland", 60)

n.add_city("Adventureland", 3060)

n.add_city("Ghosttown", 0)

n.add_city("Emerald City", 100000)

n.add_road(("Lilliput", "Gatorland"), 30)

n.add_road(("Lilliput", "Adventureland"), 18)

n.add_road(("Lilliput", "Scranton"), 11)

n.add_road(("Adventureland", "Gatorland"), 8)

n.add_road(("Adventureland", "Ghosttown"), 9)

n.add_road(("Scranton", "Emerald City"), 6)

n.add_road(("Gatorland", "Emerald City"), 9)

return n

def print_network(net):

"""

Print a listing of all cities (and their populations)in the given

network, and for each city, a listing of the cities to which it is

connected (and the distance to each). See the handout for the

exact format.

net: a Network object

returns: nothing

"""

# YOUR CODE HERE

return NotImplemented

def find_path(net, from_city, to_city):

"""

OPTIONAL CHALLENGE PROBLEM

Given that from_city and to_city are cities in the given network (net),

return a list of cities giving a path from from_city to to_city along

roads in the network.

For example, if island_net is the network shown on the handout, then

find_path(island_net, "Adventureland", "Emerald City") might return

["Adventureland", "Lilliput", "Scranton", "Emerald City"].

Does not necessarily give the shortest path!

If there is no path from from_city to to_city, or if one or both of the

given cities is not in net, returns None.

net: Network object

from_city: string

to_city: string

"""

# OPTIONAL CHALLENGE PROBLEM: YOUR CODE HERE

return NotImplemented

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!