##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
[email protected]
Ph 03 3423 121
Cell 027 3089 169
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor