[Tutor] Stack problem usind Python2.6
David Merrick
merrickdav at gmail.com
Thu Jul 21 23:16:16 CEST 2011
##from stack import Stack
class Stack:
def __init__(self):
self.items =[]
def isEmpty(self):
return self.items ==[]
def push(self,item):
self.items.append(item)
def pop(self,item):
self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol in "([{":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
top = s.pop()
if not matches(top,symbol):
balanced = False
index+=1
if balanced and s.isEmpty():
return True
else:
return False
def matches(open,close):
opens = "([{"
closers = ")]}"
opens.index(open) == closers.index(close)
symbolString = "()"
print(parChecker(symbolString))
*Output*
() returns False should be true
(() returns False which is correct
I can't find the error please help me
--
Dave Merrick
merrickdav at gmail.com
Ph 03 3423 121
Cell 027 3089 169
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110722/c6a55a68/attachment-0001.html>
More information about the Tutor
mailing list