Ted Wu
從coder變成programmer。 再從programmer變成software engineer. System designer就.......擺著當目標吧 >
class Node:
"""
Use a node to represent possibility of next letter
"""
def __init__(self, n):
self.value = n
self.children = []
def set_child(self, child):
self.children.append(child)
def get_child(self, n):
for child in self.children:
if child.value == n: return child
return None
class Condition:
"""
A class to supply a ability to retrieve letters
"""
def __init__(self):
self.root = Node('root')
def build_all_condition_node(self, current_node, arr):
if len(arr) < 2:
current_node.set_child(Node(arr[0]))
return
for c in arr:
temp_node = Node(c)
current_node.set_child(temp_node)
temp_arr = arr[:]
temp_arr.remove(c)
self.build_all_condition_node(temp_node, temp_arr)
def is_match(self, other_string):
temp_node = self.root
for c in other_string:
if not temp_node.get_child(c):
return False
else:
temp_node = temp_node.get_child(c)
return True
def build(self, target_string):
all_letter_list = list(target_string)
temp_node = self.root
self.build_all_condition_node(temp_node, all_letter_list)
condition = Condition()
condition.build('earth')
condition.is_match('heart')By Ted Wu