[Tutor] Stack problem usind Python2.6

bob gailer bgailer at gmail.com
Thu Jul 21 23:33:40 CEST 2011


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110721/fd42f7dc/attachment.html>


More information about the Tutor mailing list