-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_ladder.py
67 lines (49 loc) · 1.31 KB
/
word_ladder.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
56
57
58
59
60
61
62
63
64
65
66
# coding=utf-8
import string
all_letters = string.ascii_lowercase
start = "hit"
end = "cog"
data_list = ["hot", "dot", "dog", "lot", "log"]
"""
构造字典梯子树
hit
|
hot
|
dot lot
| |
dog log
"""
class Node(object):
def __init__(self, data, sub_node=None):
self.data = data
sub_node_list = [sub_node] if sub_node else []
self.sub_node = sub_node_list
def __repr__(self):
return '<Node(data={})>'.format(self.data)
def __str__(self):
return '<Node(data={})>'.format(self.data)
def build_tree(root):
temp_data = root.data
while data_list:
for i in range(len(temp_data)):
for char in all_letters:
new_word = temp_data[:i] + char + temp_data[i+1:]
if new_word in data_list:
new_node = Node(new_word)
root.sub_node.append(new_node)
data_list.remove(new_word)
for sub_node in root.sub_node:
build_tree(sub_node)
return
roo = Node(start)
build_tree(roo)
def my_print(node):
if not node.sub_node:
return
print(node.sub_node)
for temp in node.sub_node:
print(temp.data)
for temp in node.sub_node:
my_print(temp)
my_print(roo)