On Thu, Jul 21, 2011 at 5:16 PM, David Merrick <merrick...@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
>
> merrick...@gmail.com
>
> Ph   03 3423 121
> Cell 027 3089 169
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
























The class stack seems to basically be a list. Why not just use a list (which
is already an object) to do the things that you want it to do? All of the
methods of the class are already methods of a list, so it seems like your
creating code that is already in python (unless there is more to it than I'm
missing)


In your ParChecker function, instead of setting a variable to false, why not
add a "break" statement? This will allow you to do things like "if
s.isEmpty():" instead of "if balanced and  s.isEmpty():"



Basically, what's going on here,

        if balanced and  s.isEmpty():
            return True
        else:
            return False

so long as this s.isEmpty() evaluates to false (which it is in the first few
loops) you will get the "else" part, which is "return false".

If you throw in some print statements, like dummy prints (print index, or
print "a" or whatever) you will see how many loops it's making.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to