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+ print vertexTuple
36+ if vertexTuple not in visited :
37+ visited .add (vertexTuple )
38+ if derp (graph , vertexTuple , visited ):
39+ return True
40+ print visited
41+
42+ if len (visited ) == len (graph ):
43+ return True
44+ return False
45+
46+ def hamiltonianPath (graph ):
47+ if len (graph ) == 0 :
48+ return True
49+
50+ print graph
51+ for node in graph :
52+ if derp (graph , node ):
53+ return True
54+
55+ return False
56+
57+ def testDifferByOne ():
58+ assert not differByOne ("" , "" )
59+ assert not differByOne ("a" , "a" )
60+ assert not differByOne ("aaa" , "aaa" )
61+ assert not differByOne ("abcdeff" , "abcedff" )
62+ assert differByOne ("a" , "b" )
63+ assert differByOne ("abc" , "abb" )
64+ assert differByOne ("abc" , "bbc" )
65+ assert differByOne ("abcdefg" , "abcdefz" )
66+
67+ def testTuplelizeDuplicates ():
68+ assert tuplelizeDuplicates (["qq" , "qq" , "qq" ]) == [("qq" , 0 ), ("qq" , 1 ), ("qq" , 2 )]
69+
70+ def testCreateGraph ():
71+ assert createGraph (tuplelizeDuplicates (["aba" , "bbb" , "bab" ])) == {("aba" , 0 ): set ([]), ("bbb" , 0 ): set ([("bab" , 0 )]), ("bab" , 0 ): set ([("bbb" , 0 )])}
72+ assert createGraph (tuplelizeDuplicates (["qq" , "qq" , "qq" ])) == {('qq' , 1 ): set ([]), ('qq' , 0 ): set ([]), ('qq' , 2 ): set ([])}
73+ 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 )])}
74+
75+ def testHamiltonianPath ():
76+ assert hamiltonianPath (createGraph ([]))
77+ assert not hamiltonianPath (createGraph (["aba" , "bbb" , "bab" ]))
78+ assert hamiltonianPath (createGraph (["ab" , "bb" , "aac" ]))
79+ assert not hamiltonianPath (createGraph (["qq" , "qq" , "qq" ]))
80+ assert hamiltonianPath (createGraph (["aaa" , "aba" , "aaa" , "aba" , "aaa" ]))
81+ assert not hamiltonianPath (createGraph (["ab" , "ad" , "ef" , "eg" ]))
82+ assert hamiltonianPath (createGraph (["abc" , "abx" , "axx" , "abx" , "abc" ]))
83+ assert hamiltonianPath (createGraph (["f" , "g" , "a" , "h" ]))
84+
85+ def main ():
86+ testDifferByOne ()
87+ testTuplelizeDuplicates ()
88+ testCreateGraph ()
89+ testHamiltonianPath ()
90+
91+ if __name__ == "__main__" :
92+ main ()
0 commit comments