1+ def differByOne (word , anotherWord ):
2+ return sum (c1 != c2 for c1 , c2 in zip (word , anotherWord )) == 1
3+
4+ def tuplelizeDuplicates (inputArray ):
5+ dups = {elem :0 for elem in inputArray }
6+ outputTuples = []
7+ for elem in inputArray :
8+ if elem in dups :
9+ outputTuples .append ((elem , dups [elem ]))
10+ dups [elem ] += 1
11+ return outputTuples
12+
13+ def createGraph (inputTuples ):
14+ g = {elem :set () for elem in inputTuples }
15+
16+ size = len (inputTuples )
17+
18+ for i in xrange (size ):
19+ for j in xrange (size ):
20+
21+ if i == j :
22+ continue
23+
24+ v1 = inputTuples [i ]
25+ v2 = inputTuples [j ]
26+
27+ if differByOne (v1 [0 ], v2 [0 ]):
28+ g [v1 ].add (v2 )
29+ g [v2 ].add (v1 )
30+ return g
31+
32+ def derp (graph , startTuple , visited = set ()):
33+ # do dfs
34+ for vertexTuple in graph [startTuple ]:
35+ if vertexTuple not in visited :
36+ visited .add (vertexTuple )
37+ if derp (graph , vertexTuple , visited ):
38+ return True
39+
40+ return False
41+
42+ def hamiltonianPath (graph ):
43+ if len (graph ) == 0 :
44+ return True
45+
46+ visited = set ()
47+ for node in graph :
48+ if node not in visited and derp (graph , node , visited ):
49+ return True
50+ visited .add (node )
51+
52+ return False
53+
54+ def testDifferByOne ():
55+ assert not differByOne ("" , "" )
56+ assert not differByOne ("a" , "a" )
57+ assert not differByOne ("aaa" , "aaa" )
58+ assert not differByOne ("abcdeff" , "abcedff" )
59+ assert differByOne ("a" , "b" )
60+ assert differByOne ("abc" , "abb" )
61+ assert differByOne ("abc" , "bbc" )
62+ assert differByOne ("abcdefg" , "abcdefz" )
63+
64+ def testTuplelizeDuplicates ():
65+ assert tuplelizeDuplicates (["qq" , "qq" , "qq" ]) == [("qq" , 0 ), ("qq" , 1 ), ("qq" , 2 )]
66+
67+ def testCreateGraph ():
68+ assert createGraph (tuplelizeDuplicates (["aba" , "bbb" , "bab" ])) == {("aba" , 0 ): set ([]), ("bbb" , 0 ): set ([("bab" , 0 )]), ("bab" , 0 ): set ([("bbb" , 0 )])}
69+ assert createGraph (tuplelizeDuplicates (["qq" , "qq" , "qq" ])) == {('qq' , 1 ): set ([]), ('qq' , 0 ): set ([]), ('qq' , 2 ): set ([])}
70+ assert createGraph (tuplelizeDuplicates (["ab" , "ad" , "ef" , "eg" ])) == {('ab' , 0 ): set ([('ad' , 0 )]), ('ef' , 0 ): set ([('eg' , 0 )]), ('ad' , 0 ): set ([('ab' , 0 )]), ('eg' , 0 ): set ([('ef' , 0 )])}
71+
72+ def testHamiltonianPath ():
73+ assert hamiltonianPath (createGraph ([]))
74+ assert not hamiltonianPath (createGraph (["aba" , "bbb" , "bab" ]))
75+ assert hamiltonianPath (createGraph (["ab" , "bb" , "aac" ]))
76+ assert not hamiltonianPath (createGraph (["qq" , "qq" , "qq" ]))
77+ assert hamiltonianPath (createGraph (["aaa" , "aba" , "aaa" , "aba" , "aaa" ]))
78+ assert not hamiltonianPath (createGraph (["ab" , "ad" , "ef" , "eg" ]))
79+ assert hamiltonianPath (createGraph (["abc" , "abx" , "axx" , "abx" , "abc" ]))
80+ assert hamiltonianPath (createGraph (["f" , "g" , "a" , "h" ]))
81+
82+ def main ():
83+ testDifferByOne ()
84+ testTuplelizeDuplicates ()
85+ testCreateGraph ()
86+ testHamiltonianPath ()
87+
88+ if __name__ == "__main__" :
89+ main ()
0 commit comments