On 7/21/2011 5:16 PM, David Merrick wrote:
##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
What do you tkink matches returns, and why?
Therein is the problem.
Also FWIW you import Stack them redefine it. Why?
--
Bob Gailer
919-636-4239
Chapel Hill NC
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor