-
Notifications
You must be signed in to change notification settings - Fork 3
/
alien_language.py
executable file
·55 lines (41 loc) · 1.2 KB
/
alien_language.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
52
53
54
55
#!/usr/bin/python
# vim: foldlevel=0
"""
Given a sorted dictionary (array of words) of an alien language, find order of
characters in the language.
For example if the given arrays is {"baa", "abcd", "abca", "cab", "cad"}, then order of
characters is 'b', 'd', 'a', 'c'.
"""
from graph import Graph, Vertex
def visit(vertex, stack):
vertex.color = Vertex.GRAY
for v in vertex.neighbors:
if v.color == Vertex.WHITE:
visit(v, stack)
stack.append(vertex.key)
def build_graph(words):
graph = Graph()
for i in range(1, len(words)):
word1 = words[i-1]
word2 = words[i]
j = 0
while word1[j] == word2[j]:
j += 1
v1 = graph.add_vertex(word1[j])
v2 = graph.add_vertex(word2[j])
v1.add_neighbor(v2)
return graph
def language_order(words):
"""
>>> language_order(["baa", "abcd", "abca", "cab", "cad"])
['b', 'd', 'a', 'c']
"""
graph = build_graph(words)
stack = list()
for key, vertex in graph.vertices.iteritems():
if vertex.color == Vertex.WHITE:
visit(vertex, stack)
return list(reversed(stack))
if __name__ == "__main__":
import doctest
doctest.testmod()