python: find word

 Linked list

Queue

Binary Tree

Heap Tree

Hash Table

Sequential Search

Fibonacci

Graph

Dijkstra's 

Greedy Algorithm

 

class FindWord(object):
    """
    从字符串中找单词
    """

    def __init__(self):
        """

        """
        pass

    def isSubSequence(self,source:str,dic:list)->int:
        """
        
        :param source: 字符串
        :param dic: 要找到单词列表
        :return: 
        """
        i,j=0,0
        while i<len(source) and j<len(dic):
            if(source[i]==dic[j]):
                i+=1
                j+=1
            else:
                i+=1
        return  j==len(dic)
        
    def find(self,source:str,dic:list)->list:
        """
        
        :param source: 字符串
        :param dic: 要找到单词列表
        :return: 查找到单词列表中的存在的单词
        """
        result=[]
        for word in dic:
            if self.isSubSequence(source,word):
                result.append(word)
            
        return result
    
#test
f=FindWord()
s='bacogtadsjofisdhklasdjgckde'
d=['book','code','tag']
print(f.find(s,d))

  

from collections import deque

def palindrome(word:str)->bool:
    """
    回文
    :param word: 
    :return: 
    """
    if len(word)>1:
        wd=deque(word)
        while len(wd)>1:
            if wd.pop()!=wd.popleft():
                return False
        return True
    else:
        return False

print("x",palindrome("x"))
print('abccba',palindrome('abccba'))

  

 

class Node(object):
    """

    """
    def __init__(self,data=None):
        """

        :param data:
        """
        self.data=data
        self.left=None
        self.right=None

    def insert(self,data):
        """

        :param data:
        :return:
        """
        if self.data:
            if data<self.data:
                if self.left:
                    self.left.insert(data)
                else:
                    self.left=Node(data)
            else:
                if self.right:
                    self.right.insert(data)
                else:
                    self.right=Node(data)
        else:
            self.data=data
    
    def postorder(self):
        """
        
        :return: 
        """
        if self.left:
            self.left.postorder()
        if self.right:
            self.right.postorder()
        print(self.data)
        
    def inorder(self):
        """
        
        :param data
        :return: 
        """
        if self.left:
            self.left.inorder()
        print(self.data)
        if self.right:
            self.right.inorder()
        
        
    def search(self,val):
        """
        
        :param val: 
        :return: 
        """
        if val<self.data:
            if not self.left:
                return str(val)+"no is"
            return self.left.search(val)
        elif val>self.data:
            if not self.right:
                return str(val)+"no is"
            return self.right.search(val)
        else:
            return str(val)+"no is"
        
    
class DeleteNode(object):
    """
    
    """
    def delete(self,root:Node ,key):
        """
        
        :param root: 
        :param key: 
        :return: 
        """
        if root is None:
            return None
        if key<root.data:
            root.left=self.delete(root.left,key)
            return root
        if key>root.data:
            root.right=self.delete(root.right,key)
            return root
        if root.left is None:
            newroot=root.right
            return  newroot
        if root.right is None:
            newroot=root.right
            return newroot
        succ=self.maxnode(root.left)
        tmp=Node(succ.data)
        tmp.left=self.leftnode(root.left)
        tmp.right=root.right
        return tmp
        
        
    def leftnode(self,node:Node):
        """
        
        :param node: 
        :return: 
        """
        if node.right is None:
            newroot=node.left
            return newroot
        node.right=self.leftnode(node.right)
        return node
    
    def maxnode(self,node:Node):
        """
        
        :param node: 
        :return: 
        """
        while node.right:
            node=node.right
        return node
    
tree=Node()
datas=[10,5,21,9,13,28,3,4,1,17,32]
for d in datas:
    tree.insert(d)
tree.inorder()
print('del')
dl=5
delobj=DeleteNode()
result=delobj.delete(tree,dl)
result.inorder()

  

posted @ 2024-10-17 05:59  ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报