python代數(shù)式括號(hào)有效性檢驗(yàn)示例代碼
思路:
利用棧實(shí)現(xiàn)代數(shù)式中括號(hào)有效行的的檢驗(yàn):
代碼:
class mychain(object): #利用鏈表建立棧,鏈表為父類 length=0 def __init__(self,value=None,next=None):#創(chuàng)建鏈表,長度并不包含頭部 self.value=value self.next=next #mychain.length=mychain.length+1 def append(self,value=None): while self.next!=None: self=self.next self.next=mychain(value) mychain.length=mychain.length+1 #追加時(shí),鏈表長度增加 def travle(self):#遍歷鏈表 print(self.value) if self.next!=None: self.next.travle() def drop (self,value):#刪除特定值的第一個(gè)匹配節(jié)點(diǎn) while self.next!=None: if self.next.value!=value: self=self.next else: self.next=self.next.next mychain.length=mychain.length-1 #刪除時(shí),鏈表長度減小 break def pop(self):#刪除未節(jié)點(diǎn) if self.next!=None:#并不刪除頭結(jié)點(diǎn) while self.next.next!=None: self=self.next self.next=None mychain.length=mychain.length-1#彈出為節(jié)點(diǎn),并減小長度,頭結(jié)點(diǎn)不彈出 class stock(mychain):#棧類 bottom=None #棧底 top=None #棧頂 n_count=0 #計(jì)數(shù) def Max(self): #占中最大值 if self.next!=None: tmp = self.next.value while self.next.next!=None: self=self.next if self.next.value>tmp: tmp=self.next.value return tmp else: print('棧為空!') def Min(self):#棧中的最小值 if self.next!=None: tmp = self.next.value while self.next.next!=None: self=self.next if self.next.value<tmp: tmp=self.next.value return tmp else: print('棧為空!') def push(self,value): #壓棧 while self.next != None: self = self.next self.next = mychain(value) stock.top=self.next stock.length=stock.length+1 stock.n_count=stock.n_count+1 def __init__(self,value='',next=None): self.value=value self.next=next stock.bottom=self stock.top=self #stock.n_count=stock.n_count+1 #stock.length=stock.length+1 def append(self,value=''):#取消追加函數(shù) print('請(qǐng)使用Push()!') def pop(self): if self.next!=None:#并不刪除頭結(jié)點(diǎn) while self.next.next!=None: self=self.next self.next=None stock.top=self stock.length=stock.length-1#彈出為節(jié)點(diǎn),并減小長度,頭結(jié)點(diǎn)不彈出 class solution(object): def validationofbrackets(self,astr=''):#檢驗(yàn)串中的括號(hào)合法性 braketsstock=stock() for i in astr: if i in ['{','(','[']: braketsstock.push(i) else: if i==')': if braketsstock.top.value=='(': braketsstock.pop() else: return False elif i==']': if braketsstock.top.value=='[': braketsstock.pop() else: return False elif i=='}': if braketsstock.top.value=='{': braketsstock.pop() else: return False else: pass print(astr) print(braketsstock.length) if braketsstock.length==0: return True else: return False
運(yùn)行:
bstr='([{((({{}})))}]){{}}{{}{}{}[][]()(123)(((sin5)))}' f=solution() print(f.validationofbrackets(bstr))
總結(jié)
到此這篇關(guān)于python代數(shù)式括號(hào)有效性檢驗(yàn)的文章就介紹到這了,更多相關(guān)python代數(shù)式括號(hào)有效性檢驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?計(jì)算機(jī)視覺編程進(jìn)階之OpenCV?圖像銳化及邊緣檢測(cè)
計(jì)算機(jī)視覺這種技術(shù)可以將靜止圖像或視頻數(shù)據(jù)轉(zhuǎn)換為一種決策或新的表示。所有這樣的轉(zhuǎn)換都是為了完成某種特定的目的而進(jìn)行的,本篇我們來學(xué)習(xí)下如何對(duì)圖像進(jìn)行銳化處理以及如何進(jìn)行邊緣檢測(cè)2021-11-11Python實(shí)現(xiàn)抖音熱搜定時(shí)爬取功能
這篇文章主要為大家介紹了利用Python制作的一個(gè)新摸魚神器,可以實(shí)現(xiàn)抖音熱搜定時(shí)爬取。文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以試一試2022-03-03Python網(wǎng)絡(luò)編程 Python套接字編程
這篇文章主要為大家詳細(xì)介紹了Python網(wǎng)絡(luò)編程的相關(guān)資料,Python套接字編程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09使用matplotlib創(chuàng)建Gif動(dòng)圖的實(shí)現(xiàn)
本文主要介紹了使用matplotlib創(chuàng)建Gif動(dòng)圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04python實(shí)現(xiàn)二級(jí)登陸菜單及安裝過程
這篇文章主要介紹了python實(shí)現(xiàn)二級(jí)登陸菜單及安裝過程,,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06