-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q07_valid_build_order.py
51 lines (42 loc) · 1.55 KB
/
Q07_valid_build_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest
from collections import deque
def valid_build_order(nodes, dependencies):
graph = {}
for node_val in nodes:
graph[node_val] = Node(node_val)
for first, second in dependencies:
graph[first].add_edge(graph[second])
queue = deque([])
for node_val in nodes:
node = graph[node_val]
if not node.dependencies:
queue.append(node)
build_order = []
while queue:
node = queue.popleft()
build_order.append(node.value)
for dependent in node.edges:
dependent.dependencies -= 1
if not dependent.dependencies:
queue.append(dependent)
if len(build_order) < len(nodes):
raise Exception("No valid ordering")
return build_order
class Node:
def __init__(self, value):
self.edges = []
self.value = value
self.dependencies = 0
def add_edge(self, node):
self.edges.append(node)
node.dependencies += 1
class Test(unittest.TestCase):
def test_valid_build_order(self):
nodes = ["a", "b", "c", "d", "e", "f"]
dependencies = [("a", "d"), ("f", "b"), ("b", "d"), ("f", "a"), ("d", "c")]
expected = ["e", "f", "b", "a", "d", "c"]
self.assertEqual(expected, valid_build_order(nodes, dependencies))
invalid_dependencies = [("b", "a"), ("a", "c"), ("c", "b")]
with self.assertRaises(Exception) as context:
order = valid_build_order(nodes, invalid_dependencies)
self.assertTrue("No valid ordering" in str(context.exception))