Menu

[Solved]-Python Define New Repr Function Vertex Class Class Vertex Keep Track Vertices Connected We Q37162498

In python – define a new __repr__() function for this vertexclass

class Vertex:
”’
keep track of the vertices to which it is connected, and the weightof each edge
”’
def __init__(self, key):
”’

”’
self.ID = key
self.connected_to = {}

def add_neighbor(self, neighbor, weight=0):
”’
add a connection from this vertex to anothe
”’
self.connected_to[neighbor] = weight

def __str__(self):
”’
returns all of the vertices in the adjacency list, as representedby the connectedTo instance variable
”’
return str(self.ID) + ‘ connected to: ‘ + str([x.ID for x inself.connected_to])

def get_connections(self):
”’
returns all of the connections for each of the keys
”’
return self.connected_to.keys()

def get_ID(self):
”’
returns the current key id
”’
return self.ID

Expert Answer


Answer to In python – define a new __repr__() function for this vertex class class Vertex: ”’ keep track of the vertices to which… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *