mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-13 04:48:03 +00:00
Rework graph challenge and solution (#86)
This commit is contained in:
@@ -3,19 +3,24 @@ from nose.tools import assert_equal
|
||||
|
||||
class TestGraph(object):
|
||||
|
||||
def test_graph(self):
|
||||
def create_graph(self):
|
||||
graph = Graph()
|
||||
for id in range(0, 6):
|
||||
graph.add_node(id)
|
||||
graph.add_edge(0, 1, 5)
|
||||
graph.add_edge(0, 5, 2)
|
||||
graph.add_edge(1, 2, 3)
|
||||
graph.add_edge(2, 3, 4)
|
||||
graph.add_edge(3, 4, 5)
|
||||
graph.add_edge(3, 5, 6)
|
||||
graph.add_edge(4, 0, 7)
|
||||
graph.add_edge(5, 4, 8)
|
||||
graph.add_edge(5, 2, 9)
|
||||
return graph
|
||||
|
||||
def test_graph(self):
|
||||
graph = self.create_graph()
|
||||
|
||||
graph.add_edge(0, 1, weight=5)
|
||||
graph.add_edge(0, 5, weight=2)
|
||||
graph.add_edge(1, 2, weight=3)
|
||||
graph.add_edge(2, 3, weight=4)
|
||||
graph.add_edge(3, 4, weight=5)
|
||||
graph.add_edge(3, 5, weight=6)
|
||||
graph.add_edge(4, 0, weight=7)
|
||||
graph.add_edge(5, 4, weight=8)
|
||||
graph.add_edge(5, 2, weight=9)
|
||||
|
||||
assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)
|
||||
assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)
|
||||
@@ -29,10 +34,27 @@ class TestGraph(object):
|
||||
|
||||
print('Success: test_graph')
|
||||
|
||||
def test_graph_undirected(self):
|
||||
graph = self.create_graph()
|
||||
|
||||
graph.add_undirected_edge(0, 1, weight=5)
|
||||
graph.add_undirected_edge(0, 5, weight=2)
|
||||
graph.add_undirected_edge(1, 2, weight=3)
|
||||
|
||||
assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)
|
||||
assert_equal(graph.nodes[1].adjacent[graph.nodes[0]], 5)
|
||||
assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)
|
||||
assert_equal(graph.nodes[5].adjacent[graph.nodes[0]], 2)
|
||||
assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)
|
||||
assert_equal(graph.nodes[2].adjacent[graph.nodes[1]], 3)
|
||||
|
||||
print('Success: test_graph')
|
||||
|
||||
|
||||
def main():
|
||||
test = TestGraph()
|
||||
test.test_graph()
|
||||
test.test_graph_undirected()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user