龙空技术网

python判断段落中是否丢失括号

空山画雨 14

前言:

如今各位老铁们对“缺失右括号”可能比较关切,小伙伴们都需要了解一些“缺失右括号”的相关内容。那么小编同时在网摘上网罗了一些关于“缺失右括号””的相关知识,希望小伙伴们能喜欢,我们一起来学习一下吧!

2、链表栈模型:设计一个接口程序,判断知号是否缺失

利用栈的取值特点进行比较链式结构

'''

from stack import LStack

#调用前面文章的栈模型类

class Exeue:

def __init__(self,streue):

self.streue=streue

def exeue(self):

__sstr=LStack()

__ssts=LStack()

ssta=None

for item in self.streue:

if item=="(" or item=="[" or item=="{":

__sstr.fush(item)

if __sstr.is_empty():

if item==")" or item=="]" or item=="}":

__ssts.fush(item)

if not __sstr.is_empty():

if item==")"and __sstr.top()== "(" :

__sstr.pop()

elif item=="]"and __sstr.top()== "[" :

__sstr.pop()

elif item=="}"and __sstr.top()== "{" :

__sstr.pop()

elif item==")" or item=="]" or item=="}":

__ssts.fush(item)

if __sstr.is_empty() and __ssts.is_empty():

print("正确")

else:

print("缺少括号")

stram="(abcdmyc)]hmy"

lad=Exeue(stram)

lad.exeue()

解热法二(只能找到一个):主要看思路

from sstack import SStack

strmy="{abcmyt,kmy(ttjin)},hallo[123],tmpy end [(kkmy)]"

class Myeueu:

def __init__(self,strkmy):

self.strkmy=strkmy

self.__sstr=SStack()

self.strtty="()[]{}"

self.strlift="([{"

self.strdict={")":"(","]":"[","}":"{"}

def chaz(self):

none=0

for item in self.strkmy:

if item in self.strtty:

yield item ,none

none+=1

def mypush(self):

for iv,ik in self.chaz():

if iv in self.strlift:

self.__sstr.push((iv,ik))

elif self.__sstr.is_empty() or self.__sstr.pop(

)[0]!=self.strdict[iv]:

print("与%d位%s匹配的符号缺失"%(ik,iv))

break

else:

if self.__sstr.is_empty():

print("正确")

else:

d=self.__sstr.pop()

print("与%d位%s匹配的符号缺失"%(d[1],d[0]))

st=Myeueu(strmy)

st.mypush()

标签: #缺失右括号 #缺失右括号 梗 #缺失左括号