Skip to content

Commit 1a68eee

Browse files
committed
Update HelloGraph to show new options
1 parent 94bc758 commit 1a68eee

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

HelloGraph/challenge.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class HelloGraphChallenge(Challenge):
55
"""
6-
Given: Two nodes and a list of edges
6+
Given: Two nodes and a list of edges denoting a path
77
Return: Graph between the nodes and it's weight
88
"""
99

@@ -23,23 +23,22 @@ class HelloGraphChallenge(Challenge):
2323
'''
2424

2525
def build(self):
26+
self.model = self.lines_to_graph(start=2)
2627
self.model.start = self.line_to_integer(0)
2728
self.model.stop = self.line_to_integer(1)
28-
self.model.graph = dict()
29-
for edge in self.edges(2):
30-
self.model.graph[edge.tail] = (edge.head, edge.weight)
3129

3230
def calc(self):
33-
head = None
3431
tail = self.model.start
35-
self.result.graph = [tail]
32+
self.result.path = [tail]
3633
self.result.weight = 0
3734
while tail != self.model.stop:
38-
self.result.weight += self.model.graph[tail][1]
39-
tail = self.model.graph[tail][0]
40-
self.result.graph.append(tail)
35+
# it's a path, just one way to go
36+
head = self.model.edges[tail][0]
37+
self.result.path.append(head)
38+
self.result.weight += self.model.weights[(tail, head)]
39+
tail = head
4140

4241
def format(self):
43-
self.output = self.format_path(self.result.graph)
42+
self.output = self.format_path(self.result.path)
4443
self.output += self.br
4544
self.output += str(self.result.weight)

HelloGraph/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def test_build(self):
1616
self.challenge.build()
1717
self.assertEqual(2, self.challenge.model.start)
1818
self.assertEqual(4, self.challenge.model.stop)
19-
self.assertEqual(4, self.challenge.model.graph[3][0])
20-
self.assertEqual(10, self.challenge.model.graph[3][1])
19+
self.assertIn(4, self.challenge.model.edges[3])
20+
self.assertEqual(10, self.challenge.model.weights[(3, 4)])
2121

2222
def test_format(self):
2323
self.challenge.result.weight = 14
24-
self.challenge.result.graph = [2, 3, 4]
24+
self.challenge.result.path = [2, 3, 4]
2525
self.challenge.format()
2626
self.assertEqual(self.challenge.expectation(), self.challenge.output)
2727

0 commit comments

Comments
 (0)