[Tutor] Stack problem usind Python2.6
James Reynolds
eire1130 at gmail.com
Fri Jul 22 00:04:30 CEST 2011
On Thu, Jul 21, 2011 at 5:16 PM, David Merrick <merrickdav at gmail.com> 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
>
> --
> Dave Merrick
>
> merrickdav at gmail.com
>
> Ph 03 3423 121
> Cell 027 3089 169
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
A couple more points:
* I did just run it with the break statements where you have 'balanced =
False'
* the method Stack needs a return statement (otherwise, it will return
None), as all functions without a return statement do.
* you should either get rid of the argument for pop in Stack or make it an
optional argument (item = None) if you intended to do something with item.
* You of course need to tab in this block if you decide to go the 'break'
route:
if s.isEmpty():
return True
else:
return False
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110721/4bb11230/attachment.html>
More information about the Tutor
mailing list