python: find word

 Linked list

Queue

Binary Tree

Heap Tree

Hash Table

Sequential Search

Fibonacci

Graph

Dijkstra's 

Greedy Algorithm

 

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
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))

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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'))

  

 

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 @   ®Geovin Du Dream Park™  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-17 CSharp: Detection Platform in donet core 6
2017-10-17 sql server:alter database name
2009-10-17 yui cookie Dynamically Change Text Size Using Javascript 动态设置字体大小,写入Cookie
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示