## 十、Python語言中簡單數據結構的應用(之一)
----From a high schoolstudent's view to learn Python
關鍵字:
python 列表 堆棧數據結構 遞歸調用 函數 組合問題 后綴表達式 前綴表達式 逆波蘭表達式 運算符 優先級
?
本篇我們將簡單介紹一下數據結構中的堆棧(stack),以及在Python中利用堆棧所實現的一些應用。
一、堆棧介紹
堆棧是一個后進先出(LIFO)的數據結構,其工作方式就像往彈夾里面填子彈,在填好子彈之后,如果你要取出一顆子彈,那么你去除的這顆子彈一定是你最后填進去的,如果將子彈全部取出,那最后取出的那顆子彈一定是你最先填進去的那顆。把子彈當成我們程序中操作的元素,那么彈夾就是一個堆棧的模型了。
在棧上"push"元素是個常用術語,意思是把一個對象添加到堆棧中;反之,要刪除一個元素,你可以把它"pop"出堆棧。此外,還有”empty”來判斷堆棧是否為空狀態。
很明顯,使用Python中的List非常容易實現堆棧的模型,我們先復習一下List中有哪些內置的方法,在terminal窗口輸入:
dir(list)
['__add__', '__class__','__contains__', '__delattr__', '__delitem__', '__delslice__','__doc__', '__eq__', '__format__', '__ge__', '__getattribute__','__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__','__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__','__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__','__repr__', '__reversed__', '__rmul__', '__setattr__','__setitem__', '__setslice__', '__sizeof__', '__str__','__subclasshook__', 'append', 'count', 'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort']
內容很多,介紹一些常用的:
s[i] = x
item i of s is replaced byx
s[i:j] = t
slice of s from i to j isreplaced by the contents of the iterable t
del s[i:j]
same as s[i:j] = []
s[i:j:k] = t
the elements of s[i:j:k] arereplaced by those of t
del s[i:j:k]
removes the elements ofs[i:j:k] from the list
s.append(x)
same as s[len(s):len(s)] =[x]
s.extend(x)
same as s[len(s):len(s)] =x
s.count(x)
return number of i‘s for whichs[i] == x
s.index(x[, i[, j]])
return smallest k such thats[k] == x and i <= k < j
s.insert(i, x)
same as s[i:i] = [x]
s.pop([i])
same as x = s[i]; del s[i];return x
s.remove(x)
same as dels[s.index(x)]
s.reverse()
reverses the items of s inplace
s.sort([cmp[, key[,reverse]]])
sort the items of s inplace
我們使用List的append()來實現堆棧的push,使用pop()不帶參數的情況來實現堆棧的pop,使用len(list)==0來實現empty。
舉例:
stack=[]
len(stack)==0
True
stack.append('1')
stack.append('2')
for a in '3456789': stack.append(a)
...?
stack
['1', '2', '3', '4', '5', '6','7', '8', '9']
stack.pop()
'9'
stack
['1', '2', '3', '4', '5', '6','7', '8']
len(stack)==0
False
二、如何使用堆棧來模擬遞歸函數調用
看下面的程序,還是一個組合問題的程序:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 528.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">21</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">22</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">23</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">24</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">25</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">26</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">27</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">28</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">29</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">30</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">31</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">32</span></p></td><td valign="top" style="width: 376.9px; height: 528.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">def comb(iterable, r,order=1):</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> pool = tuple(iterable)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> if order != 1:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> pool =list(pool)</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>pool.reverse()</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> pool =tuple(pool)</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> comb=[]</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> n = len(pool)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> if r > n:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> returncomb</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> indices = range(r)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> def subcomb(N, m, n, B):</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>i=1</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> whileTrue:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> if i == m-n+2:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> break</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> indices[N-n] = i+B</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ifn-1>0:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> subcomb(N, m-i, n-1, B+i)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> else:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> comb.append(tuple(pool[k-1] for k inindices))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> i += 1</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><span style="letter-spacing: 0.0px">?<wbr>?<wbr>?<wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> subcomb(r,n,r,0)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> return comb</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "\ncomb-----"</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">comb1=comb(tuple('123456'),4)</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">for e in comb1: printe</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print len(comb1)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
這個程序就不像之前一句一句的詳細介紹了,因為使用的方法和之前的沒有根本的變化,簡單說明如下:
- 函數和之前相比,增加了一個參數,并且使用1作為缺省參數值,當該參數不為1時,我們將按照列表中元素的倒序來列出組合
- 遞歸函數為subcomb(),請注意,這個函數的定義和調用都在函數comb()中,這也是一種合法的方式
- 遞歸函數subcomb(N, m, n,B)實現的方式和前一篇介紹的稍有不同,前一篇是從后往前開始列出組合結果的,而這個是從前往后來列的,所以在參數中還額外的加了一個B用來表示遞歸子序列的起始位置;參數N表示最終組合中元素的個數,m、n分別表示當前所求的元素個數和組合中元素個數,遞歸調用時,m、n、B會變化
程序的運行結果:
comb-----
('1', '2', '3', '4')
('1', '2', '3', '5')
('1', '2', '3', '6')
('1', '2', '4', '5')
('1', '2', '4', '6')
('1', '2', '5', '6')
('1', '3', '4', '5')
('1', '3', '4', '6')
('1', '3', '5', '6')
('1', '4', '5', '6')
('2', '3', '4', '5')
('2', '3', '4', '6')
('2', '3', '5', '6')
('2', '4', '5', '6')
('3', '4', '5', '6')
15
介紹這個程序不是主要目的,主要的目的是想說明,遞歸是可以通過使用堆棧來模擬的,而且最終運行的結果也一模一樣。先看程序:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 592.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">21</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">22</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">23</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">24</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">25</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">26</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">27</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">28</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">29</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">30</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">31</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">32</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">33</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">34</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">35</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">36</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td><td valign="top" style="width: 376.9px; height: 592.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">def combusestack(iterable, r,order=1):</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> pool = tuple(iterable)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> if order != 1:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> pool =list(pool)</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>pool.reverse()</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> pool =tuple(pool)</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> comb=[]</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> nc = len(pool)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> if r > nc:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> returncomb</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> indices = range(r)</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> stack=[]</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> res=[-1,-1,-1,-1]</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> m, n, B, i = nc, r, 0,1?<wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> while True:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> while i ==m-n+2:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> if len(stack) ==0:?<wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> return comb</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr>(m,n,B,i)=stack.pop()</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> i+=1</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>indices[r-n] = i+B</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> ifn-1>0:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> res=[m, n, B, i]</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> stack.append(res)</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> m, n, B, i = m-i, n-1, B+i,1</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> continue</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> comb.append(tuple(pool[k-1]for k in indices))</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> i +=1</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><br/></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "comb usestack-----"</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">comb1=combusestack(tuple('123456'),4)</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">for e in comb1: printe</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print len(comb1)</span></p></td></tr></tbody></table>
運行結果:
comb use stack-----
('1', '2', '3', '4')
('1', '2', '3', '5')
('1', '2', '3', '6')
('1', '2', '4', '5')
('1', '2', '4', '6')
('1', '2', '5', '6')
('1', '3', '4', '5')
('1', '3', '4', '6')
('1', '3', '5', '6')
('1', '4', '5', '6')
('2', '3', '4', '5')
('2', '3', '4', '6')
('2', '3', '5', '6')
('2', '4', '5', '6')
('3', '4', '5', '6')
15
這個程序使用一個List來模擬堆棧,進入堆棧中的元素,也是一個List,我們在原來遞歸函數進行遞歸調用的地方,將函數的其中3個參數(N除外,因為在過程中不會改變)以及循環變量i,push到堆棧中,在原來函數結束的地方,在堆棧中pop一個List出來,直到堆棧空為止。
在程序中也出現了一些比較奇特的語句,如line21、line27。
這個程序的目的只是為了驗證,遞歸調用可以通過堆棧來模擬函數調用,變換為非遞歸的函數;其實這個程序可讀性不是很高,而且在line21由于堆棧的彈出,會改變循環變量i的值,在調試程序和理解程序方面會帶來困惑。
三、堆棧的簡單應用舉例
介紹兩個例子,一是利用堆棧的特性,反轉字符串,另一是利用堆棧,檢查表達式中的()是否匹配。
堆棧所具有的這種后進先出(LIFO)協議的特性,它可以作為一個通用的辦法用于反轉數據序列。例如,如果按照1,2,3的順序把值壓入一個堆棧,那么從堆棧中將它們彈出時的順序為3,2,1。
看下面字符竄反轉的例子:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 208.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td><td valign="top" style="width: 377.4px; height: 208.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">defrev(s):</font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> stack =[]</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> n =len(s)</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> if n==0: returnNone</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> for c in s:stack.append(c)</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> revstr =''</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> whilelen(stack)>0 :</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> ?<wbr>?<wbr> revstr += stack.pop()</wbr></wbr></wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">?<wbr> ?<wbr> returnrevstr</wbr></wbr></font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; min-height: 16px;"><font color="#2F3699"><br/></font></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">printrev('123456789')</font></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow';"><span style="letter-spacing: 0.0px"><font color="#2F3699">printrev('')</font></span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
運行結果如下:
987654321
None
下面是關于檢查括號是否匹配的例子
文字說明的部分摘錄于<<DataStructures and Algorithms in Python>>
In the following application, we considerarithmetic expressions that may contain various pairs of groupingsymbols, such as
? Parentheses: “(” and “)” ? Braces: “{”and “}”
? Brackets: “[” and “]”
Each opening symbol must match itscorresponding closing symbol. For example, a left bracket, “[,”must match a corresponding right bracket, “],” as in the expression[(5+x)-(y+z)]. The following examples further illustrate thisconcept:
? Correct: ()(()){([()])}
? Correct: ((()(()){([()])})) ? Incorrect:)(()){([()])}
? Incorrect: ({[])}
- Incorrect: (
程序如下:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 25.1px; height: 336.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 2.2px 1.0px 1.0px; border-color: #000000 #50a299 #000000 #000000"><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">14</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">15</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">16</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">17</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">18</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">19</span></p><p style="margin: 0px; text-align: right; line-height: normal; font-family: Arial; color: rgb(122, 122, 122);"><span style="letter-spacing: 0.0px">20</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td><td valign="top" style="width: 377.4px; height: 336.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 2.2px; border-color: #000000 #000000 #000000 #50a299"><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">defis_matched(expr):</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> """Return True if all delimiters are properlymatch;?<wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> False otherwise."""</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> lefty = '({['</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> righty = ')}]'</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> S = []?<wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> for c in expr:</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> if c inlefty:?<wbr/></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> S.append(c)</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr> elif c inrighty:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> if len(S) ==0:?<wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> return False</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> if righty.index(c) !=lefty.index(S.pop()):</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> ?<wbr> ?<wbr>?<wbr> return False</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> return len(S) == 0</wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144); min-height: 16px;"><span style="letter-spacing: 0.0px">?<wbr>?<wbr>?<wbr/></wbr></wbr></span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">printis_matched('[(5+x)-(y+z)]')</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">printis_matched('{(5+x)-(y+z)]')</span></p><p style="margin: 0px; line-height: normal; font-family: 'Arial narrow'; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">printis_matched('[(5+x)-(y+z))')</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
運行結果分別顯示為:True False False
做一些說明:
我們仍然使用List來模擬堆棧,所以push和empty的形式和真正的堆棧有差異。
程序的流程大致是這樣:
for循環掃描表達式,如果遇到的字符是’({[‘中的任一個,則將該字符壓入堆棧;否則如果字符是’)}]’中的任一個,程序先判斷當前堆棧是否為空,為空則返回錯誤,然后判斷符號是否匹配,line13的語句就是檢查是否匹配,這是本程序中唯一比較難的語句。
我的更多文章:
- [Python程序調試的一些體會](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35)
- [十四、Python編程計算24點(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euxx.html)(2013-10-03 22:18:28)
- [十三、Python編程計算24點(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)
(2013-10-02 22:15:46)
- [十二、Python簡單數據結構應用(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euk8.html)(2013-10-02 22:10:41)
- [十、Python編程解決組合問題(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)
(2013-09-21 23:37:27)
- [九、Python編程解決組合問題(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54)
- [八、Python的函數編程(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwj.html)
(2013-09-20 23:09:39)
- [七、Python的函數編程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)
(2013-09-20 23:09:10)
- [一、Python語言的入門](http://blog.sina.com.cn/s/blog_d6cca93e0101ebw4.html)(2013-09-08 09:16:19)
- [高中生如何學編程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)